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