简体   繁体   English

结构初始化器错误中的值太少

[英]Too few values in struct intializer error

i am getting the error, too few values in struct initialiser at line clusters = append(clusters, Cluster{Point{rand.Float64()}, []Point{}}) the function that throws the error is below.我收到错误,在 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
}

i am putting k = 3, the cluster struct defined is我把 k = 3,定义的集群结构是

type Cluster struct {
Center Point
Points []Point
}

and the point is also a struct defined as:并且该点也是一个结构体,定义为:

type Point struct {
X float64
Y float64
}

Can someone please help?有人可以帮忙吗?

A struct composite literal must either use named fields or specify all fields.结构复合文字必须使用命名字段或指定所有字段。 The Point struct has two fields, X and Y. Assuming that you were attempting to set the X field, do one of the following: 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

Specifying struct fields by name is generally preferred over positional values.按名称指定结构字段通常优于位置值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM