在Go中初始化结构
问题描述:
Is this valid syntax for initializing a struct in Go?
id := struct { name, ltype, value }
The fields are all strings. The actual error message I get is "syntax error: unexpected }". Maybe you cant initialize anonymous structs this way ?
在Go语言中初始化结构是否有效? p>
id:= struct {name,ltype,value}
code> pre>
所有字段均为字符串。 我收到的实际错误消息是“语法错误:意外的}”。 也许您不能以这种方式初始化匿名结构? p>
div>
答
No type inference for you!
name := "a"
ltype := "b"
value := "c"
id := struct { name, ltype, value string } { name, ltype, value }
答
You can also initialize the value inline.
id := struct{ name, ltype, value string }{"a", "b", "c"}