43

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 [][]int
}

bar := Test{foo: [[1,2,3], [1,2,3]]}
3
  • 6
    It worth reiterating that you're trying to create a slice, not array. To create an array you'd use [N]type{42, 33, 567} or [...]type{42, 33, 567} — to get the size inferred from the number of member in the initializer. Commented Oct 14, 2015 at 6:07
  • ^Sure, I guess in Go you use arrays so rarely and the syntax is so similar that I basically interchange the two even if they are different things. Commented Oct 14, 2015 at 21:12
  • I'd not say I use them rarely. In a code which parses some network protocol, it's customary to decrale an array and then reslice it multiple times -- passing those slices, being sort of windows to the underlying array, to other functions. In either case, a) I thought you could have wrong model in your head which needed fixing up ;-) b) it's better to not confuse readers which will stumble on your post later. Commented Oct 15, 2015 at 10:53

4 Answers 4

60

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.

Sign up to request clarification or add additional context in comments.

4 Comments

You don't have to use the same syntax for the inner arrays: play.golang.org/p/Wdhabb3qca
@twotwotwo true, that is technically different and imo preferred.
Glad you added the shorter example, but the answer still says "you have to use the syntax recursively. It's very verbose," and "you have to use the same syntax for the inner arrays," which seem confusing or inaccurate when you consider that inner slices can be written out in a different and shorter way.
@twotwotwo alright well I've changed my wording. I think that's nitpicking. It is the same syntax, they're both covered as 'composite literal' it's just in the case of the inner collections the type information can be inferred but using [] vs {} is really the important distinction.
7

In some other langauges (Perl, Python, JavaScript), [1,2,3] might be an array literal, but in Go, composite literals use braces, and here, you have to specify the type of the outer slice:

package main

import "fmt"

type T struct{ foo [][]int }

func main() {
    a := [][]int{{1, 2, 3}, {4, 5, 6}}
    b := T{foo: [][]int{{1, 2, 3}, {4, 5, 6}}}
    fmt.Println(a, b)
}

You can run or play with that on the Playground.

The Go compiler is just tricky enough to figure out that the elements of an [][]int are []int without you saying so on each element. You do have to write out the outer type's name, though.

Comments

6

Just replace the square brackets with curly braces. In Go, array literals are identified with curly braces.

test := [][]int{{1,2,3},{1,2,3}}

Comments

2

A slice literal is written as []type{<value 1>, <value 2>, ... }. A slice of ints would be []int{1,2,3} and a slice of int slices would be [][]int{[]int{1,2,3},[]int{4,5,6}}.

groups := [][]int{[]int{1,2,3},[]int{4,5,6}}

for _, group := range groups {
    sum := 0
    for _, num := range group {
        sum += num
    }
    fmt.Printf("The array %+v has a sum of %d\n", sub, sum)
} 

1 Comment

You don't have to specify the types of the values: play.golang.org/p/Wdhabb3qca

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.