如何在切片中设置结构体的字段?

huangapple go评论77阅读模式
英文:

How to set fields of a struct inside a slice?

问题

我声明了一个如下所示的类型:

type Klabel struct {
    values []float64
    label  string
}

然后,我创建了一个该类型的切片,如下所示:

kdata := []Klabel

我该如何设置label变量或将一系列浮点数附加到values中?

我尝试了以下方法:

kdata[0].label = "test"

kdata := make([]Klabel, 10)

kdata[0].label = "test"

以及

kdata = append(kdata[0], kdata[0].label = "test")

但是都没有成功...

所以,任何帮助都将不胜感激!提前谢谢!

英文:

I declared a type like this

type Klabel struct {
    values []float64
    label  string
}

Then, I created a slice of this type like this

kdata := []Klabel

How can I set the label variable or append a slice of floats to values?

I tried

kdata[0].label = "test"

and

kdata := make([]Klabel, 10)

kdata[0].label = "test"

and

kdata = append(kdata[0], kdata[0].label = "test")

Well, with no success...

So any help is appreciated! Thanks in advance!

答案1

得分: 5

你发布了以下内容:

kdata := make([]Klabel, 10)

kdata[0].label = "test"

它是有效的。当打印kdata切片时,输出为(Go Playground):

[{[] test} {[] } {[] } {[] } {[] } {[] } {[] } {[] } {[] } {[] }]

输出显示kdata有10个元素,第一个元素具有标签值:"test"

为了更清楚,使用以下方式打印:

fmt.Printf("%+v", kdata)

输出:

[{values:[] label:test} {values:[] label:} {values:[] label:} {values:[] label:} {values:[] label:} {values:[] label:} {values:[] label:} {values:[] label:} {values:[] label:} {values:[] label:}]

切片(与映射不同)是可寻址的。您可以通过简单地对切片进行索引并为字段分配新值来更改元素(即结构体)的字段。

例如:

kdata[0].label = "test"
kdata[0].values = []float64{1.1, 2.2}
kdata[0].values = append(kdata[0].values, 3.3)

fmt.Printf("%+v", kdata[0])

输出:

{values:[1.1 2.2 3.3] label:test}

如果您想将一个浮点数切片附加到元素的values字段:

vals := []float64{1.2, 2.3}

kdata[0].values = append(kdata[0].values, vals...) // 注意 3 个点 ...
英文:

You posted this:

kdata := make([]Klabel, 10)

kdata[0].label = "test"

And it works. When printing the kdata slice, output is (Go Playground):

[{[] test} {[] } {[] } {[] } {[] } {[] } {[] } {[] } {[] } {[] }]

The output shows kdata has 10 elements, and the first has the label value: "test".

To be more clear, print it with:

fmt.Printf("%+v", kdata)

Output:

[{values:[] label:test} {values:[] label:} {values:[] label:} {values:[] label:} {values:[] label:} {values:[] label:} {values:[] label:} {values:[] label:} {values:[] label:} {values:[] label:}]

Slices (unlike maps) are addressable. You can change the fields of the elements (that are structs) by simply indexing the slice and assigning new values to the fields.

For example:

kdata[0].label = "test"
kdata[0].values = []float64{1.1, 2.2}
kdata[0].values = append(kdata[0].values, 3.3)

fmt.Printf("%+v", kdata[0])

Output:

{values:[1.1 2.2 3.3] label:test}

If you want to append a slice of floats to the values field of an element:

vals := []float64{1.2, 2.3}

kdata[0].values = append(kdata[0].values, vals...) // Note the 3 dot ...

答案2

得分: 1

作为对另一个答案的替代方案,你也可以这样做:

kdata := []Klabel{{label: "test"}}

(在Go Playground上查看链接

但请注意,切片的长度将为1而不是10,所以并不完全等价。

英文:

As an alternative to the other answer, you could also do this:

kdata := []Klabel{{label: "test"}}

(See on Go Playground)

But note that the slice length will be 1 rather than 10, so it's not exactly equivalent.

huangapple
  • 本文由 发表于 2015年8月27日 21:08:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/32250277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定