3

I want to create a composite literal of arrays of arrays within a struct. Outside of a struct

package main

import "fmt"

func main() {
  x := [...][]string {{"a", "b"}}

  fmt.Printf("%s", x)
}

works. (http://play.golang.org/p/C2RbNnd7LL)

But I can't define a field of type [...][]string within a struct. As in http://play.golang.org/p/wHNeeuAJuO

package main

import "fmt"

type t struct {
    f    [...][]string
}

func main() {
  x := [...][]string {{"a", "b"}}
  y := t{x}
  fmt.Printf("%s", y)
}

f gives the errror use of [...] array outside of array literal

2
  • Of course it gives an error! Arrays in Go have a compile-time fixed length. As a courtesy the compiler will count elements of an array literal for you; that's the .... No literal, no counting, no ..., just state the array size or go with a slice. Have a look at blog.golang.org/slices . And please: Do not call a slice an array or vice versa (because every time someone does a kitten dies). This is just a syntax error combined with a misunderstanding of Go's arrays and slices. You finished the Go tour? Commented Nov 25, 2014 at 0:41
  • May be showing you some inadvisable patterns here, but once you do use slices, you have some options for how to write the literals: play.golang.org/p/X1Pwsp0nK2 Commented Nov 25, 2014 at 1:46

2 Answers 2

2

The problem is that [...]elementType is not a valid type name. You can use the syntax with the initialiser syntax like [...]int{1, 2, 3, 4} because the compiler can see how many elements there are to determine the actual type ([4]int in this case).

If you want an array-like type that doesn't have a fixed compile time size, use a slice:

type t struct {
    f    [][]string
}

Alternatively, if the number of elements is fixed at compile time and you still want to use an array, you will need to specify the actual element count in your type definition.

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

Comments

1

Short answer:

The notation [...] can be used to construct an array literal, but cannot be used in an array declaration. In the example you have provided, the [...] notation is used to declare a struct element. Hence the error. Replace [...] with [n], where n is the actual size of the array.

Long answer:

Unlike many other programming languages, Go includes the length of an array as part of type information. Hence, there is no type in Go that is just an array, but it is always an array of a specific size. For example, in the following code, there are two int arrays, where one is of type [3]int and the other is of type [4]int, and since they are of different types, assigning one to the other is illegal.

package main

import (
    "fmt"
    "reflect"
)

func main() {
    a := [...]int{1, 2, 3}
    b := [...]int{1, 2, 3, 4}
    fmt.Println(reflect.TypeOf(a), reflect.TypeOf(b))
}

This program prints [3]int [4]int to the console and illustrates that a and b in this program are of different types (find it here on the Go Playground). Since these are different types, assigning a to b (or vice versa) is illegal, and results in compilation error: cannot use b (type [4]int) as type [3]int in assignment

The [...] notation: The [...] can be used only as a part of a literal, and it indicates that the compiler should infer the length of array from the literal itself. This removes from a programmer the burden of counting the number of elements in the array. However, one may still specify a size in the literal, provided the literal has as many or fewer elements in it (in which case, the remaining elements in the resulting array are empty). For e.g. a := [4]int{1,2} is legal and will create this array: [1 2 0 0]

The [...] notation cannot be used in a variable declaration, and hence this statement is invalid: var x [...]int. Similarly, in the type definition of a struct of your example, this statement is illegal: f [...][]string, and requires that you specify the size of the array.

Comments

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.