Go中的数组是按值计算的?

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

从上面测试了一下,似乎Go中的数组是按值传递的,而不是按引用传递的。那么我是否可以得出结论,在 Go 中处理此类问题时不需要 shallow-copyingdeep-copying 的概念?

Go 中的一切总是按值传递,即使有指针,它也会复制该指针的 8 个字节。

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).


您应该将 Go 数组与 Go 切片进行比较。赋值复制数组值。赋值复制切片描述符值。切片描述符是一个结构,具有长度、容量和指向其底层切片数组的指针。

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

例如,

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)
}

游乐场:https://play.golang.org/p/8eFa1Mod_Kj

输出:

[1 2 3 4 5] [1 2 3 4 100]
[1 2 3 4 100] [1 2 3 4 100]

Go 中的一切都是按值传递的(Go 中没有 "pass by reference" 的概念)。

因为一切都是按值传递的,所以它被复制了。指针的副本只是地址的副本,而不是 "deep copy"。因此,没有直接或隐藏指针的对象的副本将是深副本,而具有显式或隐式指针的对象的副本将是浅副本。

数组不包含指针,因此如果您的数组元素不包含指针,您将获得深拷贝。

Slices and Channels and Maps do 包含(隐藏的)指针并复制它们会给你一个 Slice (Channel, Map) 的副本但是一个浅层的:里面的东西不会被复制,因为它们不是 Slice 的直接部分(Slice 包含指向实际数据的指针)。

所以Go中有深拷贝和浅拷贝的概念。粗略地说:一切都是深度复制的,指针的深度复制只是地址的副本(不是指针):深度复制停止在指针处(甚至隐藏在语言中,如 Slices)。

像往常一样,当 Go 中的指针是显式时,复制的内容是非常明显和确定的。