Go的函数参数总是传值

caiyang101 · · 1735 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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

有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:caiyang101

查看原文:Go的函数参数总是传值

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1735 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传