Go的函数参数总是传值

依照Go的 FQA, 函数的参数传递总是传值的(pass-by-value): As in all languages in the C family, everythi

依照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 sectionfor 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.

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

packagemainimport("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:0xc420074188inti:0xc4200741b8inti:0xc420074188string s: 0xc4200741c0string s data: 0xa778dstring s: 0xc4200741e0string s data: 0xa778dstring s: 0xc4200741c0string s data: 0xa778dslice s: 0xc42006e0c0slice s data: 0xc4200780c0slice s: 0xc42006e0e0slice s data: 0xc4200780c0slice s: 0xc42006e0c0slice s data: 0xc4200780c0struct s: 0xc420074210, I:0xc420074210struct s: 0xc420074218, I:0xc420074218struct s: 0xc420084020, I:0xc420074210inti:0xc420074188inti:0xc420074230struct s: 0xc420074210, I:0xc420074210struct s: 0xc420074228, I:0xc420074228struct s: 0xc420074210, I:0xc420074210
未登录用户
全部评论0
到底啦