Go语言中结构体方法副本传参与指针传参的区别介绍

Rebecca ·
更新时间:2024-09-20
· 817 次阅读

GO语言结构体方法跟结构体指针方法的区别

首先,我定了三个接口、一个结构和三个方法:

type DeptModeA interface { Name() string SetName(name string) } type DeptModeB interface { Relocate(building string, floor uint8) } type Dept struct { name string building string floor uint8 Key string } func (self Dept) Name() string { return self.name } func (self Dept) SetName(name string) { self.name = name } func (self *Dept) Relocate(building string, floor uint8) { self.building = building self.floor = floor }

而后我写了一些测试代码:

dept1 := Dept{ name: "MySohu", building: "Internet", floor: 7} switch v := interface{}(dept1).(type) { case DeptModeFull: fmt.Printf("The dept1 is a DeptModeFull.\n") case DeptModeB: fmt.Printf("The dept1 is a DeptModeB.\n") case DeptModeA: fmt.Printf("The dept1 is a DeptModeA.\n") default: fmt.Printf("The type of dept1 is %v\n", v) } deptPtr1 := &dept1 if _, ok := interface{}(deptPtr1).(DeptModeFull); ok { fmt.Printf("The deptPtr1 is a DeptModeFull.\n") } if _, ok := interface{}(deptPtr1).(DeptModeA); ok { fmt.Printf("The deptPtr1 is a DeptModeA.\n") } if _, ok := interface{}(deptPtr1).(DeptModeB); ok { fmt.Printf("The deptPtr1 is a DeptModeB.\n") }

打印出的内容:

The dept1 is a DeptModeA.
 The deptPtr1 is a DeptModeFull. 
The deptPtr1 is a DeptModeA. 
The deptPtr1 is a DeptModeB.

假设T是struct,那么Go里面遵循下面几个原则:

T的方法集仅拥有 T Receiver (方法中的接受者)方法。 *T 方法集则包含全部方法 (T + *T)。

所以你上面的例子dept1应该是拥有方法:Name和SetName

而&dept1拥有方法:Name、SetName和Relocate

这个就是Go里面在设计方法的时候需要注意Receiver的类型

Go语言中结构体方法副本传参与指针传参的区别

我们来看个例子:

package main import ( "fmt" ) type B struct { Name string } func(b B) Test1() { fmt.Printf("Test1 addr:%p\n", &b) fmt.Printf("Test1 name:%s\n", b.Name) b.Name = "john" } func(b *B) Test2() { fmt.Printf("Test2 addr:%p\n", b) fmt.Printf("Test2 name:%s\n", b.Name) b.Name = "john" } func main() { b := B{} b.Test1() b.Test1() b.Test2() b.Test2() }

执行后结果如下:

Test1 addr:0xc42000e1e0 Test1 name: Test1 addr:0xc42000e1f0 Test1 name: Test2 addr:0xc42000e1d0 Test2 name: Test2 addr:0xc42000e1d0 Test2 name:john

可以看到Test1中打印出b结构体的地址在变化,而Test2中没有变化,这说明每一次Test1的调用,都是传入的结构体b的一个副本(拷贝),当在Test1中对内部变量的任何改动,都将会失效(因为下一次访问的时候传入的是b结构体新的副本)。而Test2方法作为指针传参时,每一次传入的都是b结构体的指针,指向的是同一个结构体,因此地址没有变化,且对内部变量做改动时,都是改动的b结构体内容。

在Go语言中的这个差别可能是对OOP设计的一个坑,在Go语言中要想实现OOP的设计,在进行方法封装时,都采用Test2的写法。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对软件开发网的支持。

您可能感兴趣的文章:Go语言结构体定义和使用方法go语言通过反射获取和设置结构体字段值的方法Go语言中的匿名结构体用法实例Go语言指针访问结构体的方法浅谈Go语言中的结构体struct & 接口Interface & 反射Go语言里的结构体文法实例分析Go语言到底有没有引用传参(对比 C++ )



GO 方法 结构体 指针 go语言

需要 登录 后方可回复, 如果你还没有账号请 注册新账号