2

What's the difference between func foo(arr []int) int and func foo(arr [*num*]int) int?

Here are two examples:

func foo1(arr [2]int) int {
    arr[0] = 1
    return 0
}

func foo2(arr []int) int {
    arr[0] = 1
    return 0
}

func main() {
    var arr1 = [2]int{3, 4}
    var arr2 = []int{3, 4}
    foo1(arr1)
    println(arr1[0])      // result is 3, so arr in foo1(arr) is a copy
    foo2(arr2)
    println(arr2[0])      // result is 1, so arr in foo2(arr) is not a copy, it is a reference
}

I also found if I use foo1(arr2) or foo2(arr1), the compiler will report an error like "cannot use arr2 (type []int) as type [2]int in argument to foo1" and "cannot use arr1 (type [2]int) as type []int in argument to foo2".

So who can help to explain what's the difference between them or give me some link to study? Thank you in advance.

icza
  • 389,944
  • 63
  • 907
  • 827
user3300252
  • 109
  • 6
  • 1
    Links to study: Tour of Go https://tour.golang.org/welcome/1 .The FAQ https://golang.org/doc/faq . Effective Go https://golang.org/doc/effective_go.html . The Language Spec https://golang.org/ref/spec and the Blog https://blog.golang.org/. Such basic question are all covered in detail in the Tour: Take it twice. – Volker Jun 23 '17 at 08:30

3 Answers3

5

[2]int is an array, []int is a slice.

Arrays and slices are completely different types: you can't pass an array where a slice is required, and you can't pass a slice where an array is expected. Since the length is part of the array type, you can't even use array values where the length differs, e.g. you can't use an array value of type [3]int for something that expects [2]int.

Everything in Go is passed by value. Slices too. But a slice value is a header, describing a contiguous section of a backing array, and a slice value only contains a pointer to the array where the elements are actually stored. The slice value does not include its elements (unlike arrays). When you pass a slice, only the slice header is copied (pointing to the same backing array), and so modifying its elements modifies elements in the same backing array, so the caller will observe the changes. Read more about this here: Are golang slices pass by value? To see what's in a slice header: reflect.SliceHeader.

Unlike slices, arrays are not headers. An array value means all its elements, so when you pass an array value, all its elements are copied, and inside the function it is passed to you can only modify this copy array; the caller won't observe the changes made to the array (e.g. changing its elements).

Note that however it is very easy to obtain a slice value from an array, you may simply use slicing:

var a [2]int = [2]int{1, 2}
var s []int = a[:]
fmt.Println(s) // Prints [1 2]

Recommended blog post: Go Slices: usage and internals

More insight into arrays vs slices: Why have arrays in Go?

icza
  • 389,944
  • 63
  • 907
  • 827
0

[2]int is a fixed-length array with 2 entries.
[]int is a not fixed slice.

func foo1(arr [2]int) expects a fixed-length array of int with 2 entries. func foo2(arr []int) takes a non-fixed slice of int with any number of entries.

They look similar, but it may help if you think of [2]int and []int as totally different structures.

0

The type [n]T is an array of n values of type T.

The type []T is a slice with elements of type T.

An array has a fixed size. whereas a slice is dynamic.

With foo1 a copy of the array is made when the function is called.

So the original value arr1 remains unchanged

Jack Evans
  • 1,697
  • 3
  • 17
  • 33