golang如何解决import cycle not allowed的问题

1.import cycles now allowed是如何产生的?

假设存在两个package a,b

package main

import (
	"tg/a"
)

func main() {
	a.A()
}
package a

import (
	"fmt"
	"tg/b"
)

type As struct {
	Aa string
}

func A() {
	fmt.Println("this is a")
	bs := b.Bs{"bbb"}
	fmt.Println(bs)
}
package b

import (
	"fmt"
	"tg/a"
)

type Bs struct {
	Bb string
}

func B() {
	fmt.Println("this is b")
	as := a.As{"aaa"}
}

import cycle not allowed
package main
        imports tg/a    
        imports tg/b    
        imports tg/a

2.解决方式

采用分包的方式引入package c 将公用的部分提取出来

然后在package a和b中分别引入package c。

猜你喜欢

转载自blog.csdn.net/coffiasd/article/details/114748400
今日推荐