Golang中如何避免嵌入字段导致的崩溃问题

Golang中如何避免嵌入字段导致的崩溃问题 我想避免这段代码崩溃,并希望通过这个接口打印结果。我尝试了很多方法但都没成功。我的目标是在函数2中调用函数1,反之亦然,为了避免空指针解引用,我在主包中写了

function1.MyInterfaces{AllInterfaces: allInterfaces1}

但我不知道为什么它仍然崩溃且不打印结果。

以下是接口包:

package interfaces

type AllInterfaces interface {
    Function1()
    Function2()
}

以下是主包:

package main

import (
    function1 "github.com/ibilalkayy/test/two/directory1"
    function2 "github.com/ibilalkayy/test/two/directory2"
)

func main() {
    allInterfaces1 := function1.MyInterfaces{}.AllInterfaces
    allInterfaces2 := function2.MyInterfaces{}.AllInterfaces

    myFunction1 := function1.MyInterfaces{AllInterfaces: allInterfaces1}
    myFunction2 := function2.MyInterfaces{AllInterfaces: allInterfaces2}

    myFunction1.Function1()
    myFunction2.Function2()
}

以下是 function1 包

package function1

import (
    "fmt"

    "github.com/ibilalkayy/test/two/interfaces"
)

type MyInterfaces struct {
    AllInterfaces interfaces.AllInterfaces
}

func (m MyInterfaces) Function1() {
    fmt.Println("This is the function 1")
    m.CallFunction2()
}

func (m MyInterfaces) CallFunction2() {
    m.AllInterfaces.Function2()
}

以下是 function2 包

package function2

import (
    "fmt"

    "github.com/ibilalkayy/test/two/interfaces"
)

type MyInterfaces struct {
    AllInterfaces interfaces.AllInterfaces
}

func (m MyInterfaces) CallFunction1() {
    m.AllInterfaces.Function1()
}

func (m MyInterfaces) Function2() {
    m.CallFunction1()
    fmt.Println("This is the function 2")
}

更多关于Golang中如何避免嵌入字段导致的崩溃问题的实战教程也可以访问 https://www.itying.com/category-94-b0.html

9 回复

我会重新处理我的项目,看看它是否能正常工作。

更多关于Golang中如何避免嵌入字段导致的崩溃问题的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


那么你可以查看这个仓库:flow

你没有嵌入任何东西,看起来你误用了接口。接口描述了一个类型为实现该接口而应实现的函数。另一方面,嵌入是一种“子类化”,用于实现Go语言视角下的继承。

你好,在 main 函数中,那些是 nil,即接口的零值。

ibilalkayy:

    allInterfaces1 := function1.MyInterfaces{}.AllInterfaces
    allInterfaces2 := function2.MyInterfaces{}.AllInterfaces

但是我不知道如何用填充的值来调用它,因为即使我写

function1.MyInterfaces{}.AllInterfaces.Function1()

它也会给我这个结果:

function1.MyInterfaces{}.AllInterfaces.Function1() (no value) used as valuecompilerTooManyValues func (interfaces.AllInterfaces) Function1()

我的解决方案从一开始就使用了您给出的示例。在这种情况下,您可以尝试添加一个三维接口并嵌入另外两个。例如,标准库中的 io.ReadWriter

type TotalBudget interface {
    TotalAmount
    BudgetAmount
}

没有看到具体的代码和逻辑很难下定论。但如果您遇到了导入循环问题,那可能意味着项目设计存在更深层次的问题。

我试图解决的一个主要问题是:

我有名为 TotalAmountBudgetAmount 的接口。

我在 TotalAmount 接口中编写 TotalAmount 方法,在 BudgetAmount 接口中编写 BudgetAmount 方法,然后有时这两个方法会相互调用,因为它们相互依赖,这导致了导入循环。我不知道从接口的角度如何避免这种情况。

因为你提供的解决方案是将方法分成两个模块,但我的情况不同,其中模块1依赖于模块2,反之亦然。

我希望你理解了我关于在接口中处理导入循环的观点。

也许你可以更具体地说明你想要实现什么,这样我就能帮助你?

更新:如果你只是需要基于当前代码的解决方案,可以这样做:

main.go

package main

import (
	".../function1"
	".../function2"
)

func main() {
	allInterfaces1 := &function1.MyInterfaces{}
	allInterfaces2 := &function2.MyInterfaces{}

	allInterfaces1.AllInterfaces = allInterfaces2
	allInterfaces2.AllInterfaces = allInterfaces1

	allInterfaces1.Function2()
	allInterfaces2.Function1()
}

interface.go

package interfaces

type AllInterfaces interface {
	Function1()
	Function2()
}

function1.go

package function1

import (
	"fmt"

	".../interfaces"
)

type MyInterfaces struct {
	AllInterfaces interfaces.AllInterfaces
}

func (m MyInterfaces) Function1() {
	fmt.Println("This is function1 from module 1")
}

func (m MyInterfaces) Function2() {
	fmt.Println("I am function2 from module 1")
	m.AllInterfaces.Function2()
}

function2.go

package function2

import (
	"fmt"

	".../interfaces"
)

type MyInterfaces struct {
	AllInterfaces interfaces.AllInterfaces
}

func (m MyInterfaces) Function1() {
	fmt.Println("I am function1 from module 2")
	m.AllInterfaces.Function1()
}

func (m MyInterfaces) Function2() {
	fmt.Println("This is function2 from module 2")
}

输出:

% go run main.go 
I am function2 from module 1
This is function2 from module 2
I am function1 from module 2
This is the function 1 from module 1

你的代码崩溃是因为在 main 包中,你创建了 MyInterfaces 结构体,但其中的 AllInterfaces 字段是 nil。当你调用 CallFunction2() 时,它尝试调用 m.AllInterfaces.Function2(),而 m.AllInterfacesnil,导致空指针解引用崩溃。

要解决这个问题,你需要确保 AllInterfaces 字段被正确初始化。这里的关键是让 function1.MyInterfacesfunction2.MyInterfaces 互相引用,形成一个循环依赖。这可以通过在初始化时传递彼此的实例来实现。

以下是修改后的代码示例:

主包:

package main

import (
    function1 "github.com/ibilalkayy/test/two/directory1"
    function2 "github.com/ibilalkayy/test/two/directory2"
)

func main() {
    // 创建两个 MyInterfaces 实例
    myFunction1 := function1.MyInterfaces{}
    myFunction2 := function2.MyInterfaces{}

    // 互相设置 AllInterfaces 字段
    myFunction1.AllInterfaces = myFunction2
    myFunction2.AllInterfaces = myFunction1

    // 调用函数
    myFunction1.Function1()
    myFunction2.Function2()
}

function1 包:

package function1

import (
    "fmt"

    "github.com/ibilalkayy/test/two/interfaces"
)

type MyInterfaces struct {
    AllInterfaces interfaces.AllInterfaces
}

func (m MyInterfaces) Function1() {
    fmt.Println("This is the function 1")
    m.CallFunction2()
}

func (m MyInterfaces) CallFunction2() {
    if m.AllInterfaces != nil {
        m.AllInterfaces.Function2()
    }
}

function2 包:

package function2

import (
    "fmt"

    "github.com/ibilalkayy/test/two/interfaces"
)

type MyInterfaces struct {
    AllInterfaces interfaces.AllInterfaces
}

func (m MyInterfaces) CallFunction1() {
    if m.AllInterfaces != nil {
        m.AllInterfaces.Function1()
    }
}

func (m MyInterfaces) Function2() {
    m.CallFunction1()
    fmt.Println("This is the function 2")
}

修改说明:

  1. main 包中,先创建 myFunction1myFunction2 实例,然后互相设置它们的 AllInterfaces 字段。
  2. function1function2 包中,添加了 nil 检查,避免在 AllInterfacesnil 时调用方法。

这样修改后,Function1()Function2() 可以互相调用,并且不会因为空指针解引用而崩溃。

回到顶部