How to create Slice of Struct in Golang?
Example
package main
import (
"fmt"
)
type Widget struct {
id int
attrs []string
}
func main() {
widgets := []Widget{
Widget{
id: 10,
attrs: []string{"blah", "foo"},
},
Widget{
id: 11,
attrs: []string{"foo", "bar"},
},
Widget{
id: 12,
attrs: []string{"xyz"},
},
}
for _, j := range widgets {
fmt.Printf("%d ", j.id)
for _, y := range j.attrs {
fmt.Printf(" %s ", y)
}
fmt.Println()
}
}
10 blah foo
11 foo bar
12 xyz
Most Helpful This Week
Example of Fscan, Fscanf, and Fscanln from FMT Package
How do you handle HTTP responses in Go?
Program in Go language to Calculate Average Using Arrays
Illustration of Checkpoint Synchronization in Golang
How to read current directory using Readdir?
How to add items to Slice using append function in Golang?