Reputation: 830
I have a deeply nested struct which contains two slices, as seen below:
package main
import "fmt"
type bar struct {
v1 []int
v2 []int
}
type foo struct{ bar bar }
type tar struct{ foo foo }
func main() {
f := &tar{foo: foo{bar: bar{v1: [2], v2: [3]}}}
fmt.Printf("Hello, playground %s", f)
}
How do I initialize the two slices? Or how do I get this code working?
Here is the Golang Play for it: http://play.golang.org/p/zLutROI4YH.
Upvotes: 2
Views: 3002
Reputation: 8394
It's possible with []int{1,2,3}
notation, example (solves your problem):
&tar{foo: foo{bar: bar{v1: []int{2}, v2: []int{2}}}}
P.S. I strongly advise you to read The Go Programming Language Specification and FAQ section.
Upvotes: 7
Reputation: 6767
v1
and v2
are slices. The way you initialize those is with make([]int, YOUR_INITIAL_SIZE)
instead of [2]
and [3]
.
Upvotes: 1