Go的函数参数总是传值

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"reflect"
"unsafe"
)
func printInt(i int) {
fmt.Printf("int i: %p\n", &i)
}
func printInt2(i *int) {
fmt.Printf("int i: %p\n", i)
}
func printStr(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)
}
func printStr2(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)
}
func printSlice(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)
}
func printSlice2(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)
}
type S struct {
I int
}
func printStruct(s S) {
fmt.Printf("struct s: %p, I: %p\n", &s, &(s.I))
}
func printStruct2(s *S) {
fmt.Printf("struct s: %p, I: %p\n", &s, &(s.I))
}
func printInterface(i interface{}) {
fmt.Printf("int i: %p\n", &i)
}
func printInterface2(i interface{}) {
s := i.(S)
fmt.Printf("struct s: %p, I: %p\n", &s, &(s.I))
}
func printInterface3(i interface{}) {
s := i.(*S)
fmt.Printf("struct s: %p, I: %p\n", s, &(s.I))
}
func main() {
//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)
}

输出结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
int i: 0xc420074188
int i: 0xc4200741b8
int i: 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
int i: 0xc420074188
int i: 0xc420074230
struct s: 0xc420074210, I: 0xc420074210
struct s: 0xc420074228, I: 0xc420074228
struct s: 0xc420074210, I: 0xc420074210

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

本文来自:鸟窝

感谢作者:smallnest

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

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

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