How to initialize a struct containing a slice of structs in Golang?
Example
package main
import (
"fmt"
"math/rand"
)
type LuckyNumber struct {
number int
}
type Person struct {
lucky_numbers []LuckyNumber
}
func main() {
tmp := make([]LuckyNumber, 10)
for i := range tmp {
tmp[i].number = rand.Intn(100)
}
a := Person{tmp}
fmt.Println(a)
}
{[{81} {87} {47} {59} {81} {18} {25} {40} {56} {0}]}
Most Helpful This Week
Illustration of Cigarette Smokers Problem in Golang
Avoid Unintended Variable Shadowing in Golang
Program in Go language to Calculate Standard Deviation using Math package
Golang program for implementation of Shell Sort
Undefined reference to <variable/function> error in Golang
Copy Struct Type Using Value and Pointer Reference