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
Missing return at end of function error in Golang
Example of Interface with Type Embedding and Method Overriding in GO language
Syntax error: unexpected <token> error in Golang
GO Program to Find the Largest Number Among Three Numbers
GO language program with example of String Compare function
GO Hello World program