结构初始化错误中的值太少 [英] Too few values in struct intializer error

查看:54
本文介绍了结构初始化错误中的值太少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了错误,线簇的结构初始化程序中的值太少= 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.

这篇关于结构初始化错误中的值太少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆