小编wil*_*est的帖子

golang new 一个结构体但返回一个接口

这是golang代码,func newXXX返回一个接口,但为什么它不返回一个struct

type _ABitOfEverythingServer struct {
    v map[string]*examples.ABitOfEverything
    m sync.Mutex
}

type ABitOfEverythingServer interface {
    examples.ABitOfEverythingServiceServer  // interface
    examples.StreamServiceServer            // interface
}

func newABitOfEverythingServer() ABitOfEverythingServer { 
//<-why not return _ABitOfEverythingServer, is it a good way?
    return &_ABitOfEverythingServer{
        v: make(map[string]*examples.ABitOfEverything),
    }
}
Run Code Online (Sandbox Code Playgroud)

go

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

go ×1