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
Golang program to print a matrix in Spiral Format
How to Convert string to float type in Go?
Print inverted full pyramid using star
Illustration of Sleeping Barber Problem in Golang
GO Program to Check Whether a Number is Even or Odd
Program in Go language to print Floyd's Triangle
How To Make HTTP Requests in Go?
How do you handle HTTP client caching in Go?
How to change slice item value in Golang?
How to get first and last element of slice in Golang?