如何在go中初始化具有基础字符串值的类型?

时间:2014-11-12 16:20:37

标签: go

这有效:

type T string
var t T = "hello"

http://play.golang.org/p/275jQ4ixvp

但是cannot use s (type string) as type T in assignment

失败了
type T string
s := "world"
var t T = s

http://play.golang.org/p/vm3mC5ltcE

如何让第二种情况发挥作用?

1 个答案:

答案 0 :(得分:4)

将字符串转换为正确的类型[conversions]

http://play.golang.org/p/dkavI_QgPb

s := "world"
t := T(s)
fmt.Println(t)