小编典典

为什么可以键入别名函数并在不进行强制转换的情况下使用它们?

go

在Go中,如果您定义新类型,例如:

type MyInt int

然后,您不能将a传递给MyInt需要int的函数,反之亦然:

func test(i MyInt) {
    //do something with i
}

func main() {
    anInt := 0
    test(anInt) //doesn't work, int is not of type MyInt
}

精细。但是,为什么同样的不适用于功能呢?例如:

type MyFunc func(i int)
func (m MyFunc) Run(i int) {
    m(i)
}

func run(f MyFunc, i int) {
    f.Run(i)
}

func main() {
    var newfunc func(int) //explicit declaration
    newfunc = func(i int) {
        fmt.Println(i)
    }
    run(newfunc, 10) //works just fine, even though types seem to differ
}

现在,我没有抱怨,因为它使我不必像在第一个示例中那样必须显式转换newfunc为type
MyFunc。看起来似乎不一致。我敢肯定有充分的理由。谁能启发我?

我问的原因主要是因为我想以这种方式缩短一些较长的函数类型,但是我想确保这样做是可以预期的并且可以接受的:)


阅读 376

收藏
2020-07-02

共1个答案

小编典典

事实证明,这是我对Go如何处理类型的一种误解,可以通过阅读规范的相关部分来解决:

http://golang.org/ref/spec#Type_identity

我不知道的相关区别是 命名未命名 类型的区别。

命名 类型是具有名称的类型,例如int,int64,float,string,bool。此外,您使用“ type”创建的任何类型都是命名类型。

未命名的 类型是诸如[] string,map [string] string,[4] int之类的类型。它们没有名称,只是对应于其结构的描述。

如果比较两个命名类型,则名称必须匹配,以便它们可以互换。如果您比较命名类型和未命名类型,则 只要基础表示形式匹配 ,就可以了!

例如,给定以下类型:

type MyInt int
type MyMap map[int]int
type MySlice []int
type MyFunc func(int)

以下是无效的:

var i int = 2
var i2 MyInt = 4
i = i2 //both named (int and MyInt) and names don't match, so invalid

以下很好:

is := make([]int)
m := make(map[int]int)
f := func(i int){}

//OK: comparing named and unnamed type, and underlying representation
//is the same:
func doSlice(input MySlice){...}
doSlice(is)

func doMap(input MyMap){...}
doMap(m)

func doFunc(input MyFunc){...}
doFunc(f)

我有点不知所措,我不早知道,所以我希望可以为其他人澄清一下百灵鸟!而且意味着比我最初想像的要少得多的铸造:)

2020-07-02