请解释 golang 类型是否按值传递

Please explain if golang types pass by value

我正在尝试制作一个非常简单的程序来修改数组,但是如果我将它们转换为类型,运行 会出现一些有趣的行为。 https://play.golang.org/p/KC7mqmHuLw 看来,如果我有一个数组,则按引用传递,但如果我有一个类型,则按值传递。这是正确的吗?

我有两个变量 b 和 c,都是 3 个整数的数组,但是 c 是 cT 类型,在其他方面它们应该是相同的。我可以将值分配为 b[0]=-1c[0]=-1,但是如果我将这些数组作为参数传递给函数,它们的行为就会大不相同。

程序的输出是:

before b: [1 2 3]

before c: [1 2 3]

*after b: [-1 2 0]

*after c: [-1 2 3]

*what? c: [-1 2 0]

我最初的假设是 "after b" 和 "after c" 行应该是相同的。我是不是做错了什么或者我是否纠正了按值传递给函数的类型(即在传递给函数之前创建变量的副本)?

package main

import "fmt"

type cT [3]int

func main() {
    b := []int{1, 2, 3}
    c := cT{1, 2, 3}

    fmt.Println("before b:", b)
    fmt.Println("before c:", c)

    b[0] = -1
    c[0] = -1
    mangleB(b) // ignore return value
    mangleC(c) // ignore return value

    fmt.Println("*after b:", b)
    fmt.Println("*after c:", c)

    c = mangleC(c)    
    fmt.Println("*what? c:", c)    
}

func mangleB(row []int) []int {
    row[2] = 0
    return row
}

func mangleC(row cT) cT{
    row[2] = 0
    return row
}

The Go Programming Language Specification

Array types

An array is a numbered sequence of elements of a single type, called the element type.

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.

Calls

In a function call, the function value and arguments are evaluated in the usual order. After they are evaluated, the parameters of the call are passed by value to the function and the called function begins execution. The return parameters of the function are passed by value back to the calling function when the function returns.


type cT [3]int

b := []int{1, 2, 3}
c := cT{1, 2, 3}

I have two variables, b and c, both are arrays of 3 integers


不,你没有!

bint 切片 ,长度 (len(b)) 3,容量 (cap(b)) 3, c 是 (len(c)) 3 int.

数组

在Go中,所有参数都是按值传递的。 b 作为切片描述符传递,c 作为数组传递。切片描述符是一个 struct,具有切片长度和容量,以及指向底层数组的指针。

查看评论:

func main() {
    b := []int{1, 2, 3} // slice
    c := cT{1, 2, 3} // array

    fmt.Println("before b:", b) 
    fmt.Println("before c:", c) 

    b[0] = -1
    c[0] = -1

    // passing in a slice which you can think of as ref to array
    // pass by value, and it is copy of ref to array 
    mangleB(b) // ignore return value

    // passing in copy of array (pass by value)
    // yes full shallow copy of array 
    mangleC(c) // ignore return value
    // if you ignore return modifications are lost

    fmt.Println("*after b:", b)
    fmt.Println("*after c:", c)

    // return value is modified array
    c = mangleC(c)
    // c now copy of array from line 24

    fmt.Println("*what? c:", c)
}

https://play.golang.org/p/OAaCMhc-Ug