【发布时间】:2014-04-29 05:07:54
【问题描述】:
是否可以在new 类型的声明中包含值。
type Vertex struct {
X, Y int
}
func main() {
v := new( Vertex{ 0, 0} ) // Like so
fmt.Println( v )
// Instead of :
v = new(Vertex)
v.X, v.Y = 12, 4 // extra line for initializing the values of X and Y
fmt.Println( v )
}
或者因为 go 使 "Vertex{val, val} " 成为文字值而不是基本的 Vertex 类型是不可能的?
【问题讨论】:
-
是的,有可能,您可以随时声明
new对象,只需我的关键字new。谷歌文档。
标签: go