第一种方式:抽象出来一个接口层:

golang不允许循环import package ,如果检测到 import cycle ,会在编译时报错,通常import cycle是因为设计错误或包的规划问题。

以下面的例子为例,package a依赖package b,同事package b依赖package a

package aimport ( "fmt" "github.com/mantishK/dep/b")type A struct {}func (a A) PrintA() { fmt.Println(a)}func NewA() *A { a := new(A) return a}func RequireB() { o := b.NewB() o.PrintB()}

package b:

package bimport ( "fmt" "github.com/mantishK/dep/a")type B struct {}func (b B) PrintB() { fmt.Println(b)}func NewB() *B { b := new(B) return b}func RequireA() { o := a.NewA() o.PrintA()}

就会在编译时报错:

import cycle not allowed
package github.com/mantishK/dep/a
  imports github.com/mantishK/dep/b
  imports github.com/mantishK/dep/a

现在的问题就是:

A depends on B
B depends on A

那么如何避免?

引入package i, 引入interface

package itype Aprinter interface { PrintA()}

让package b import package i

package bimport ( "fmt" "github.com/mantishK/dep/i")func RequireA(o i.Aprinter) { o.PrintA()}

引入package c

package cimport ( "github.com/mantishK/dep/a" "github.com/mantishK/dep/b")func PrintC() { o := a.NewA() b.RequireA(o)}

现在依赖关系如下:

A depends on B
B depends on I
C depends on A and B

第二种方式:建立一个组合子包

  1. type CombileAB struct {

  2. A *package_a.PackageA

  3. B *package_b.PackageB

  4. }

  5.  

第三种方式:通过callback的方式回调,把函数通过参数传入

第四种方式:通过事件总线(eventBus)解耦

 

参考文章: