dongweihuan8610 2018-07-05 03:29
浏览 25
已采纳

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

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?

  • 写回答

3条回答 默认 最新

  • douan3019 2018-07-05 03:46
    关注

    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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题