golang中函数参数的可赋值性

时间:2014-04-08 21:16:05

标签: go

Runnable on playground

type Boolean bool

func takes_bool(b bool) {
    fmt.Printf("%t\n", b)
}

func takes_boolean(b Boolean) {
    fmt.Printf("%t\n", b)
}

当我调用以下内容时:

takes_bool(Boolean(false))
takes_bool(Boolean(true))

我明白了:

cannot use Boolean(false) (type Boolean) as type bool in function argument
cannot use Boolean(true) (type Boolean) as type bool in function argument

关于可转让性的规则似乎 NOT 不允许它,即至少有一个不是命名类型&两者都有相同的基础类型:

type Boolean bool

vs

bool

1 个答案:

答案 0 :(得分:2)

仔细阅读http://golang.org/ref/spec#Types后,似乎bool被视为命名类型(intfloat以及朋友)。短语"未命名的类型"仅引用interface{}struct{}等类型文字。

相关问题