go struct作为值函数参数需要注意的地方
2013-12-07 00:00
351 查看
摘要: struct作为值函数参数需要注意的地方
struct作为值函数参数需要注意的地方,则是copy一份, 但是struct 里面的指针则也是copy地址,
则改变原来结构中指针指向的数据, 将相应改变copy之后里面的指针指向的值, 因为它们指向的是同一个地址.
package main
import (
"fmt"
)
type test struct {
id int32
name string
}
type test2 struct {
id int32
t *test
}
func main() {
var t2 = test2{
id: 1,
t: &test{
id: 2,
name: "she",
},
}
t3 := t2
fmt.Println(t2.id, *t2.t)
fmt.Println(t3.id, *t3.t)
t3.t.id = 9
fmt.Println(t2.id, *t2.t)
fmt.Println(t3.id, *t3.t)
}
输出:
1 {2 she}
1 {2 she}
1 {9 she}
1 {9 she}
需要注意指针问题.
struct作为值函数参数需要注意的地方,则是copy一份, 但是struct 里面的指针则也是copy地址,
则改变原来结构中指针指向的数据, 将相应改变copy之后里面的指针指向的值, 因为它们指向的是同一个地址.
package main
import (
"fmt"
)
type test struct {
id int32
name string
}
type test2 struct {
id int32
t *test
}
func main() {
var t2 = test2{
id: 1,
t: &test{
id: 2,
name: "she",
},
}
t3 := t2
fmt.Println(t2.id, *t2.t)
fmt.Println(t3.id, *t3.t)
t3.t.id = 9
fmt.Println(t2.id, *t2.t)
fmt.Println(t3.id, *t3.t)
}
输出:
1 {2 she}
1 {2 she}
1 {9 she}
1 {9 she}
需要注意指针问题.
相关文章推荐
- C# 引用类型作为函数参数时一些需要注意的地方
- struct位域,对齐以及union作为class需要注意的地方
- C#的Replace函数,使用函数作为的参数需要注意
- 指针作为函数参数传递时需要注意的问题
- 实例解析使用指针作为函数参数传递需要注意的问题
- C++指针作为函数的参数进行传递时需要注意的一些问题
- synchronized修饰函数和封装变量的对象两处需要注意的地方
- 写正确函数需要注意的地方:二分查找
- 不定输入参数函数的编程中需要用到的宏,即注意事项
- 写正确函数需要注意的地方:在字符串中找出连续最长的数字串,并把这个串的长度返回
- 写正确函数需要注意的地方:链表的合并排序
- 使用函数模板需要注意的几个地方
- go语言将函数作为参数传递
- c和c++关于struct需要注意的地方
- Oracle分析函数model使用时需要注意的几个地方(一)
- 智能指针变量做函数参数的一个值得注意的地方
- 写正确函数需要注意的地方:输入一个字符串,打印出该字符串中字符的所有排列
- go map操作需要注意的地方
- Go 的垃圾回收机制在实践中有哪些需要注意的地方(转)
- 智能指针变量做函数参数的一个值得注意的地方