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 do you handle HTTP authentication with an HTTP client in Go?
Add Method to Struct Type
Exploring Blockchain: Top 15 Real-World Use Cases in 2024
Golang program for implementation of Comb Sort
How to add items to Slice using append function in Golang?
GO Program to Swap Number Without Using Temporary Variables