[英]Go build with protocol buffer error: too few values in struct initializer
[英]Too few values in struct intializer error
我收到错误,在 struct initialiser 行中的值太少 cluster = append(clusters, Cluster{Point{rand.Float64()}, []Point{}}) 引发错误的函数如下。
func initClusters(k int) (clusters []Cluster) {
rand.Seed(time.Now().UnixNano())
for i := 0; i < k; i++ {
clusters = append(clusters, Cluster{Point{rand.Float64()},[]Point{}})
}
return
}
我把 k = 3,定义的集群结构是
type Cluster struct {
Center Point
Points []Point
}
并且该点也是一个结构体,定义为:
type Point struct {
X float64
Y float64
}
有人可以帮忙吗?
结构复合文字必须使用命名字段或指定所有字段。 Point 结构有两个字段,X 和 Y。假设您试图设置 X 字段,请执行以下操作之一:
Point{X: rand.Float64()} // Y defaults to zero value
Point(X: rand.Float64(), Y: 0} // Y explicitly set to zero using name
Point(rand.Float64(), 0} // Y explicitly set to zero using positional value
按名称指定结构字段通常优于位置值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.