请说明golang类型是否按值传递

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

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

程序的输出为:</ p>


b之前:[1 2 3] </ p>
\ c之前的n

:[1 2 3] </ p>

* b之后的c:[-1 2 0] </ p>

* c之后的c:[- 1 2 3] </ p>

*什么? c:[-1 2 0] </ p>
</ blockquote>

我最初的假设是,“ b之后”和“ c之后”行应该相同。 我是做错了事还是对通过值传递给函数的类型是正确的(即在传递给函数之前创建变量的副本)?</ p>

  package main 
\ nimport“ fmt”

type cT [3] int

func main(){
b:= [] int {1,2,3}
c:= cT {1,2,3}
\ n fmt.Println(“在b:”之前,b)
fmt.Println(“在c:”之前,c)

b [0] = -1
c [0] = -1
mangleB(b )//忽略返回值
mangleC(c)//忽略返回值

fmt.Println(“ *之后b:”,b)
fmt.Println(“ *之后c:”,c)\ n
c = mangleC(c)
fmt.Println(“ * what?c:”,c)
}

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

返回行
}

func mangleC(row cT)cT {
row [2] = 0
返回行
}
</ code> </ pre>
</ div>

展开原文

原文

I'm trying to make a very simple program that modifies arrays, but ran across some interesting behavior if I converted them to types. https://play.golang.org/p/KC7mqmHuLw It appears that if I have an array go passes by reference, but if I have a type then go passes by value. Is this correct?

I have two variables b and c, both are arrays of 3 integers, but c is of type cT, in every other respect they should be identical. I can assign values as b[0]=-1 and c[0]=-1, but if I pass those arrays as parameters to a function they act very differently.

The output of the program is:

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]

My initial assumption is that the lines "after b" and "after c" should have been the same. Am I doing something incorrectly or am I correct about types passing to functions by value (ie. creating copy of the variable before passing to the function)?

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
}

dongwo5449
dongwo5449 好的,谢谢您的帮助!通过提供的信息,我通过传递对类型manglePC(&c)的引用并将函数更改为:funcmanglePC(row*cT){(*row)[2]=0}获得所需的效果。
接近 3 年之前 回复

2个回答




Go编程语言规范 </ p>

数组类型< / a> </ p>

数组是单一类型元素的编号序列,称为
元素类型。 </ p>

切片类型 </ p>
\ n

切片是基础
数组连续段的描述符,并提供对该
数组中元素编号序列的访问。</ p>

调用 </ p>

在函数调用中,函数值和参数在
通常的顺序。 求值后,调用的参数将按值传递给函数,并且被调用的函数开始执行。 函数返回时,函数的返回参数由值
传递回调用函数。</ p>
</ blockquote>



 类型cT [3] int 

b:= [] int {1、2、3}
c:= cT {1、2、3}
</ code> </ pre>

我有两个变量, b </ code>和 c </ code>,它们都是3个整数的数组</ p>
</ blockquote>


不,你不知道!</ p>

b </ code>是 int <的切片</ strong> / code>,其长度为( len(b)</ code>)3,容量为( cap(b)</ code>)3, c </ code>是一个数组 </ strong> of( len(c)</ code>)3 int </ code>。</ p>

在Go中,所有参数均按值传递。 b </ code>作为切片描述符传递, c </ code>作为数组传递。 切片描述符是具有切片长度和容量以及指向底层数组的指针的 struct </ code>。</ p>
</ div>

展开原文

原文

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


No, you don't!

b is a slice of int with length (len(b)) 3 and capacity (cap(b)) 3, c is an array of (len(c)) 3 int.

In Go, all parameters are passed by value. b is passed as a slice descriptor, c is passed as an array. A slice descriptor is a struct with a slice length and capacity, and a pointer to the underlying array.



查看评论:</ p>

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

fmt.Println(“之前 b:“,b)
fmt.Println(” c之前“,c)

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

//传递您所需要的切片 可以认为是对数组的ref
//通过值传递,并且它是对ref的数组
的副本
mangleB(b)//忽略返回值

//传递数组的副本(按值传递)

//是数组的完整浅表副本
mangleC(c)//忽略返回值
//如果忽略返回值,则会丢失

fmt.Println(“ * after b:”,b)\ n fmt.Println(“ * after c:”,c)

//修改返回值array
c = mangleC(c)
// c现在从第24行复制数组

fmt。 Println(“ * what?c:”,c)
}
</ code> </ pre>

展开原文

原文

See comments:

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问