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

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

问题描述:

我遇到了错误,线簇的结构初始化程序中的值太少= append(clusters,Cluster {Point {rand.Float64()},[] Point {}})引发错误的函数如下.

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.

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,定义的簇结构为

i am putting k = 3, the cluster struct defined is

type Cluster struct {
Center Point
Points []Point
}

,该点也是定义为:

type Point struct {
X float64
Y float64
}

有人可以帮忙吗?

结构复合文字必须使用命名字段或指定所有字段.Point结构具有两个字段X和Y.假设您尝试设置X字段,请执行以下操作之一:

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: 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.