GopherGolangimport cycle not allowedinterface分包定义接口

目录

1. 应用场景

假设有如下使用场景:

AABCBCACA
2. 代码实现

其程序大致如下:

package a
package a

import (
    "fmt"

    "github.com/ggq89/mutualdep/b"
    "github.com/ggq89/mutualdep/c"
)

type A struct {
    Pb *b.B
    Pc *c.C
}

func New(ic int) *A {
    a := &A{
        Pc: c.New(ic),
    }

    a.Pb = b.New(a)

    return a
}

func Printf(v int) {
    fmt.Printf("%v", v)
}
package b
package b

import (
    "github.com/ggq89/mutualdep/a"
)

type B struct {
    Pa *a.A
}

func New(a *a.A) *B {
    return &B{
        Pa: a,
    }
}

func (b *B) DisplayC() {
    b.Pa.Pc.Show()
}
package c
package c

import "github.com/ggq89/mutualdep/a"

type C struct {
    Vc int
}

func New(i int) *C {
    return &C{
        Vc: i,
    }
}

func (c *C) Show() {
    a.Printf(c.Vc)
}
package apackage bpackage cpackage bpackage apackage cpackage a
main
package main

import "github.com/ggq89/mutualdep/a"

func main() {
    a := a.New(3)
    a.Pb.DisplayC()
}

编译时就会报错如下:

import cycle not allowed
package main
    imports github.com/ggq89/mutualdep/a
    imports github.com/ggq89/mutualdep/b
    imports github.com/ggq89/mutualdep/a
3. 定义接口

现在的问题是:

A depends on B 
B depends on A
A structB struct
package ba interfacebaaa interface
package b
package b

import (
    "github.com/ggq89/mutualdep/c"
)

type B struct {
    Pa a
}

type a interface {
    GetC() *c.C
}

func New(a a) *B {
    return &B{
        Pa:a,
    }
}

func (b *B) DisplayC() {
    b.Pa.GetC().Show()
}
package a
package a
package a

import (
    "fmt"

    "github.com/ggq89/mutualdep/b"
    "github.com/ggq89/mutualdep/c"
)

type A struct {
    Pb *b.B
    Pc *c.C
}

func New(ic int) *A {
    a := &A{
        Pc:c.New(ic),
    }

    a.Pb = b.New(a)

    return a
}

func (a *A)GetC() *c.C {
    return a.Pc
}

func Printf(v int)  {
    fmt.Printf("%v", v)
}
4. 拆分包

再次编译,提示如下:

import cycle not allowed
package main
    imports github.com/ggq89/mutualdep/a
    imports github.com/ggq89/mutualdep/b
    imports github.com/ggq89/mutualdep/c
    imports github.com/ggq89/mutualdep/a

现在是另一个相互依赖问题:

A depends on C 
C depends on A
A structB struct
package cpackage a
  • 这种相互依赖可以通过将方法拆分到另一个包的方式来解决;在拆分包的过程中,可能会将结构体的方法转化为普通的函数;
package ff
package f

import "fmt"

func Printf(v int) {
    fmt.Printf("%v", v)
}
package fpackage a
package a

import (
    "github.com/ggq89/mutualdep/b"
    "github.com/ggq89/mutualdep/c"
)

type A struct {
    Pb *b.B
    Pc *c.C
}

func New(ic int) *A {
    a := &A{
        Pc: c.New(ic),
    }

    a.Pb = b.New(a)

    return a
}

func (a *A) GetC() *c.C {
    return a.Pc
}
package cpackage f
package c

import (
    "github.com/ggq89/mutualdep/a/f"
)

type C struct {
    Vc int
}

func New(i int) *C {
    return &C{
        Vc: i,
    }
}

func (c *C) Show() {
    f.Printf(c.Vc)
}

现在依赖关系如下:

A depends on B and C
B depends on C
C depends on F 

至此,两种包相互依赖关系都得以解决。

5. 总结
  1. 对于软相互依赖,利用分包的方法就能解决,有些函数导致的相互依赖只能通过分包解决;分包能细化包的功能;

  2. 对于硬相互依赖只能通过定义接口的方法解决;定义接口能提高包的独立性,同时也提高了追踪代码调用关系的难度;


参考文章: