How do I create an array of int arrays in Golang using slice literals?
I've tried
test := [][]int{[1,2,3],[1,2,3]}
and
type Test struct {
foo [][]iint
}
bar := Test{foo: [[1,2,3], [1,2,3]]}
How do I create an array of int arrays in Golang using slice literals?
I've tried
test := [][]int{[1,2,3],[1,2,3]}
and
type Test struct {
foo [][]iint
}
bar := Test{foo: [[1,2,3], [1,2,3]]}
You almost have the right thing however your syntax for the inner arrays is slightly off, needing curly braces like; test := [][]int{[]int{1,2,3},[]int{1,2,3}}
or a slightly more concise version; test := [][]int{{1,2,3},{1,2,3}}
The expression is called a 'composite literal' and you can read more about them here; https://golang.org/ref/spec#Composite_literals
But as a basic rule of thumb, if you have nested structures, you have to use the syntax recursively. It's very verbose.