Array of struct in Go language

arrays, go, initialization, struct

Solution

You are initializing cities as a slice of nodes with one element (an empty node).

You can initialize it to a fixed size with `cities := make([]node,47)`, or you could initialize it to an empty slice, and `append` to it:

cities := []node{}
for i := 0; i<47 ;i++ {
  n := node{name: strconv.Itoa(i), children: map[string]int{}}
  cities = append(cities,n)
}

I'd definitely recommend reading this article if you are a bit shaky on how slices work.

Problem

I am new to Go and want to create and initialise an struct array in go. My code is like this ``` type node struct { name string children map[string]int } cities:= []node{node{}} for i := 0; i<47 ;i++ { cities[i].name=strconv.Itoa(i) cities[i].children=make(map[string]int) } ``` I get the following error: ``` panic: runtime error: index out of range goroutine 1 [running]: panic(0xa6800, 0xc42000a080) ``` Please help. TIA :)

Original source