Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays in Go are by value?

package main

import (
  "fmt"
)

func main() {
  var a = [5]int{1,2,3,4,5}
  b := a
  b[4] = 100
  fmt.Println(a,b) //[1 2 3 4 5] [1 2 3 4 100]
}

Doing a test from above, it seems that arrays in Go are passed by value instead of reference. So can I conclude that there is no concept of shallow-copying nor deep-copying needed when dealing with such matter in Go?

like image 963
Isaac Avatar asked Jul 05 '18 03:07

Isaac


People also ask

Are arrays passed by value in Go?

In Go, there is no such thing as passing by reference. Everything is passed by value. If you assign the value of an array to another variable, the entire value is copied. In memory allocation, and in function, the array is actually a really simple data type, and works in much the same way as structs.

Does array contain value Golang?

Arrays in Golang are simply sequences or homogeneous collections of elements with similar data types in the memory. The values of the array are called elements or items. Arrays can contain zero or more than zero values.

How do I read an array in Golang?

In Go language, arrays are mutable, so that you can use array[index] syntax to the left-hand side of the assignment to set the elements of the array at the given index. You can access the elements of the array by using the index value or by using for loop. In Go language, the array type is one-dimensional.


1 Answers

The Go Programming Language Specification

Array types

An array is a numbered sequence of elements of a single type, called the element type. The number of elements is called the length and is never negative.

The length is part of the array's type; it must evaluate to a non-negative constant representable by a value of type int. The length of array a can be discovered using the built-in function len. The elements can be addressed by integer indices 0 through len(a)-1.

Slice types

A slice is a descriptor for a contiguous segment of an underlying array and provides access to a numbered sequence of elements from that array. A slice type denotes the set of all slices of arrays of its element type. The value of an uninitialized slice is nil.

Like arrays, slices are indexable and have a length. The length of a slice s can be discovered by the built-in function len; unlike with arrays it may change during execution. The elements can be addressed by integer indices 0 through len(s)-1. The slice index of a given element may be less than the index of the same element in the underlying array.

A slice, once initialized, is always associated with an underlying array that holds its elements. A slice therefore shares storage with its array and with other slices of the same array; by contrast, distinct arrays always represent distinct storage.

The array underlying a slice may extend past the end of the slice. The capacity is a measure of that extent: it is the sum of the length of the slice and the length of the array beyond the slice; a slice of length up to that capacity can be created by slicing a new one from the original slice. The capacity of a slice a can be discovered using the built-in function cap(a).


You should compare Go arrays to Go slices. Assignment copies the array value. Assignment copies the slice descriptor value. The slice descriptor is a struct with a length, a capacity, and a pointer to its underlying slice array.

type slice struct {
    array unsafe.Pointer
    len   int
    cap   int
}

For example,

package main

import "fmt"

func main() {
    // array
    var a = [5]int{1, 2, 3, 4, 5}
    b := a
    b[4] = 100
    fmt.Println(a, b)

    // slice
    var s = []int{1, 2, 3, 4, 5}
    t := s
    t[4] = 100
    fmt.Println(s, t)
}

Playground: https://play.golang.org/p/8eFa1Mod_Kj

Output:

[1 2 3 4 5] [1 2 3 4 100]
[1 2 3 4 100] [1 2 3 4 100]
like image 94
peterSO Avatar answered Oct 22 '22 16:10

peterSO