Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
golang新建一个结构,但返回一个接口_Go - Fatal编程技术网

golang新建一个结构,但返回一个接口

golang新建一个结构,但返回一个接口,go,Go,下面是golang代码,func newXXX返回一个接口,但为什么它不返回一个结构 type _ABitOfEverythingServer struct { v map[string]*examples.ABitOfEverything m sync.Mutex } type ABitOfEverythingServer interface { examples.ABitOfEverythingServiceServer // interface exam

下面是golang代码,func newXXX返回一个接口,但为什么它不返回一个结构

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),
    }
}

首先,你需要学习围棋的基础知识。拿着

简化一个复杂的例子

package main

import "fmt"

type S struct{ F int }

type I interface{}

func newI() I {
    return &S{F: 42}
}

func main() {
    i := newI()
    s := i.(*S)
    f := s.F
    fmt.Println(f)
}
游乐场:

输出:

42

newI函数返回接口类型I的值,其中包含具体类型*S的值。

首先,您需要学习Go的基础知识。拿着

简化一个复杂的例子

package main

import "fmt"

type S struct{ F int }

type I interface{}

func newI() I {
    return &S{F: 42}
}

func main() {
    i := newI()
    s := i.(*S)
    f := s.F
    fmt.Println(f)
}
游乐场:

输出:

42

newI函数返回接口类型I的值,其中包含具体类型*S的值。

我不知道在上述代码段中返回接口是否有任何特殊原因

但一般来说,返回structs是一种方法。还记得接受接口,返回结构吗

返回接口无论如何都不会简化模拟,因为客户机可以在需要模拟时定义接口,这是golang接口最漂亮的地方

func newABitOfEverythingServer() *_ABitOfEverythingServer { // <- returning struct ptr
    return &_ABitOfEverythingServer{
        v: make(map[string]*examples.ABitOfEverything),
    }
}
自从模拟实现以来,在测试拖缆时,这大大简化了模拟 不必实现examples.ABitOfEverythingServiceServer接口


来自Java、C等背景的开发人员通常会误解这一点,因为类型系统是基于名称的,而不像Go那样是结构化的。因为在这些语言中,客户端无法修改接口,所以它接受该接口,因为这需要将implements NewInterfacedFinedByClient子句添加到所有需要传递给客户端的类中。

我不知道在上述代码段中返回接口是否有任何特殊原因

但一般来说,返回structs是一种方法。还记得接受接口,返回结构吗

返回接口无论如何都不会简化模拟,因为客户机可以在需要模拟时定义接口,这是golang接口最漂亮的地方

func newABitOfEverythingServer() *_ABitOfEverythingServer { // <- returning struct ptr
    return &_ABitOfEverythingServer{
        v: make(map[string]*examples.ABitOfEverything),
    }
}
自从模拟实现以来,在测试拖缆时,这大大简化了模拟 不必实现examples.ABitOfEverythingServiceServer接口


来自Java、C等背景的开发人员通常会误解这一点,因为类型系统是基于名称的,而不像Go那样是结构化的。因为在这些语言中,客户端无法修改它接受的接口,因为这需要将implements NewInterfacedFinedByClient子句添加到所有需要传递给客户端的类中。

关于为什么的问题?关于人工代码片段没有答案。可能是技术原因,或者是出于教育目的,或者是为了展示一些东西。或者这是一个品味的问题。暗中摸索,但因为ABitOfEverythingServer是一个接口,如果您想交换它,它更灵活,很容易模拟或插入测试。我建议阅读有关接口的内容。关于原因的问题?关于人工代码片段没有答案。可能是技术原因,或者是出于教育目的,或者是为了展示一些东西。或者这是一个品味的问题。暗中摸索,但因为ABitOfEverythingServer是一个接口,如果您想交换它,它更灵活,很容易模拟或插入测试。我建议阅读关于接口的文章。这个答案是屈尊的。这个答案是屈尊的。