剑客
关注科技互联网

Go的函数参数总是传值

依照Go的 FQA
, 函数的参数传递总是传值的(pass-by-value):

As in all languages in the C family, everything in Go is passed by value. That is, a function always gets a copy of the thing being passed, as if there were an assignment statement assigning the value to the parameter. For instance, passing an int value to a function makes a copy of the int, and passing a pointer value makes a copy of the pointer, but not the data it points to. (See a later section
for a discussion of how this affects method receivers.)

Map and slice values behave like pointers: they are descriptors that contain pointers to the underlying map or slice data. Copying a map or slice value doesn’t copy the data it points to. Copying an interface value makes a copy of the thing stored in the interface value. If the interface value holds a struct, copying the interface value makes a copy of the struct. If the interface value holds a pointer, copying the interface value makes a copy of the pointer, but again not the data it points to.

以下是测试代码,结果贴在下面,不解释了。

packagemain

import(
"fmt"
"reflect"
"unsafe"
)

funcprintInt(iint) {
 fmt.Printf("int i: %p/n", &i)
}

funcprintInt2(i *int) {
 fmt.Printf("int i: %p/n", i)
}

funcprintStr(sstring) {
 fmt.Printf("string s: %p/n", &s)

 hdr := (*reflect.StringHeader)(unsafe.Pointer(&s))
 data := hdr.Data
 fmt.Printf("string s data: 0x%x/n", data)

}

funcprintStr2(s *string) {
 fmt.Printf("string s: %p/n", s)
 hdr := (*reflect.StringHeader)(unsafe.Pointer(s))
 data := hdr.Data
 fmt.Printf("string s data: 0x%x/n", data)
}

funcprintSlice(s []int) {
 fmt.Printf("slice s: %p/n", &s)

 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&s))
 data := hdr.Data
 fmt.Printf("slice s data: 0x%x/n", data)

}

funcprintSlice2(s *[]int) {
 fmt.Printf("slice s: %p/n", s)
 hdr := (*reflect.SliceHeader)(unsafe.Pointer(s))
 data := hdr.Data
 fmt.Printf("slice s data: 0x%x/n", data)
}

typeSstruct{
 I int
}

funcprintStruct(s S) {
 fmt.Printf("struct s: %p, I: %p/n", &s, &(s.I))
}

funcprintStruct2(s *S) {
 fmt.Printf("struct s: %p, I: %p/n", &s, &(s.I))
}

funcprintInterface(iinterface{}) {
 fmt.Printf("int i: %p/n", &i)
}

funcprintInterface2(iinterface{}) {
 s := i.(S)
 fmt.Printf("struct s: %p, I: %p/n", &s, &(s.I))
}

funcprintInterface3(iinterface{}) {
 s := i.(*S)
 fmt.Printf("struct s: %p, I: %p/n", s, &(s.I))
}

funcmain() {
//test int
 i :=10
 fmt.Printf("int i: %p/n", &i)
 printInt(i)
 printInt2(&i)

//test string
 s := "hello, world"
 fmt.Printf("/n/nstring s: %p/n", &s)
 hdr := (*reflect.StringHeader)(unsafe.Pointer(&s))
 data := hdr.Data
 fmt.Printf("string s data: 0x%x/n", data)
 printStr(s)
 printStr2(&s)

//slice and map
 sl := []int{1,2,3,4,5}
 fmt.Printf("/n/nslice s: %p/n", &sl)
 hdr2 := (*reflect.SliceHeader)(unsafe.Pointer(&sl))
 data = hdr2.Data
 fmt.Printf("slice s data: 0x%x/n", data)
 printSlice(sl)
 printSlice2(&sl)

//struct
 ss := S{I:10}
 ssp := &ss
 fmt.Printf("/n/nstruct s: %p, I: %p/n", ssp, &(ss.I))
 printStruct(ss)
 printStruct2(ssp)

//interface to int
 fmt.Printf("/n/nint i: %p/n", &i)
 printInterface(i)
//interface to struct
 fmt.Printf("/n/nstruct s: %p, I: %p/n", ssp, &(ss.I))
 printInterface2(ss)
 printInterface3(ssp)
}

输出结果:

inti:0xc420074188
inti:0xc4200741b8
inti:0xc420074188


string s: 0xc4200741c0
string s data: 0xa778d
string s: 0xc4200741e0
string s data: 0xa778d
string s: 0xc4200741c0
string s data: 0xa778d


slice s: 0xc42006e0c0
slice s data: 0xc4200780c0
slice s: 0xc42006e0e0
slice s data: 0xc4200780c0
slice s: 0xc42006e0c0
slice s data: 0xc4200780c0


struct s: 0xc420074210, I:0xc420074210
struct s: 0xc420074218, I:0xc420074218
struct s: 0xc420084020, I:0xc420074210


inti:0xc420074188
inti:0xc420074230


struct s: 0xc420074210, I:0xc420074210
struct s: 0xc420074228, I:0xc420074228
struct s: 0xc420074210, I:0xc420074210
分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址