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
How to use WaitGroup to delay execution of the main function until after all goroutines are complete.
How do you handle HTTP server HTTP/2 in Go?
Is There Still a Need for Programming in This AI World?
Golang program for implementation of Floyd–Warshall Algorithm
Top 20 Most Popular Cryptocurrencies To Watch In 2024
How do you handle HTTP server health checks in Go?