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
10 Countries Paying Highest Salaries to Cybersecurity Professionals
How do you handle HTTP client server alerting in Go?
How to check if an item exists in Slice in Golang?
Cannot use <variable> as <type> value in return statement error in Golang
Syntax error: unexpected <token> error in Golang
How do you create an HTTP server in Go?