Too few values in struct intializer error

后端 未结 1 649
一个人的身影
一个人的身影 2021-01-29 16:09

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

相关标签:
1条回答
  • 2021-01-29 17:09

    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.

    0 讨论(0)
提交回复
热议问题