求问Go语言圣经的一个问题

MrDoubleU · 2018-06-18 19:00:25 · 1131 次点击

改成下面可能更清楚:

We make an exception to this rule when an interface is satisfied by a single concrete type but that type cannot live in the same package where the type(interface) is needed because of their dependencies. In that case, an interface is a good way to decouple two packages.

package A

import "B"
//import "C"

type TA int

FuncA(o *B.ClassB) {
    o.Hello()
}

/*应当用这个来解耦A和B的相互依赖
FuncA(o C.InterfaceHello) {
    o.Hello()
}*/



package B

import "A"

var ta A.TA = 0

type ClassB struct {

}

func(self *ClassB) Hello() {

}

func test() {
    o := &ClassB{}
    A.FuncA(o)
}



package C

type InterfaceHello interface {
    Hello()
}
#5
更多评论
root666
自带排他气质

原文在此 1.png 同问~~

#1

> 这个具体类型不能和这个接口存在在一个相同的包中

比如,接口在 a 包定义的,具体类型在 b 包定义的。

#2