dongpu4141 2019-01-10 13:52
浏览 25

我正在使用界面,如何解决导入周期不允许的问题?

Three structures are there: A(package a),B(package b),C(package c).

B wants to use functionality of C and C wants to use functionality of B. A has both B and C instances, so that B can access functionality of C via A and vice versa.

I used an interface Ageter declared in another package i which has function declaration as GetA() *a.A Now I am using this interface Ageter in B and C through which I get instance of A and access functionality of C and B respectively.

package a

import (
    "fmt"

    "basics/importCycleIssue/issueFix/b"
    "basics/importCycleIssue/issueFix/c"
)

type A struct {
    B *b.B
    C *c.C
}
var a = NewA()

func NewA() *A {
    a := &A{}
    a.B = b.NewB(a)
    a.C = c.NewC(a)
    return a
}
func GetA() *A{
    return a
}

---------------------------------------------------
package b

import (
    "fmt"

    "basics/importCycleIssue/issueFix/i"
)

type B struct {
    o i.Ageter
}

func NewB(o i.Ageter) *B {
    b := &B{o: o}
    return b
}

func (b *B) UseC() {
    fmt.Println("need to use C:",b.o.GetA().C)
}
----------------------------------------------------
package c

import (
    "fmt"

    "basics/importCycleIssue/issueFix/i"
)

type C struct {
    o i.Ageter
}

func NewC(o i.Ageter) *C {
    c := &C{o: o}
    return c
}

func (c *C) UseB() {
    fmt.Println("need to use B:",c.o.GetA().B)
}
----------------------------------------------------
package i

import (
    "basics/importCycleIssue/issueFix/a"
)

type Aprinter interface {
    PrintA()
}
type Ageter interface {
    GetA() *a.A
}
---------------------------------------------------
package main

import (
    "basics/importCycleIssue/issueFix/a"
)

func main() {
    o := a.NewA()
    o.B.UseC()
    o.C.UseB()
}

I should be able to use functionality of B in C and vice versa.

While building the code I am getting import cycle not allowed error. import cycle not allowed package main imports basics/importCycleIssue/issueFix/a imports basics/importCycleIssue/issueFix/b imports basics/importCycleIssue/issueFix/i imports basics/importCycleIssue/issueFix/a Can anyone tell me how to fix this problem?

Thanks.

  • 写回答

2条回答 默认 最新

  • douyi8760 2019-01-10 14:05
    关注

    Go doesn’t allow import cycles to occur. If there are any import cycles detected, it throws a compile time error. Generally import cycles are considered as a bad design.

    There are different approaches to solve this, for example you can use same package to describe all these 3 types in different 3 files:

    package types
    
    type A struct {
        B *b.B
        C *c.C
    }
    
    type B struct {
        o i.Ageter
    }
    
    type C struct {
        o i.Ageter
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料