Go语言中结构体方法副本传参与指针传参的区别介绍
GO语言结构体方法跟结构体指针方法的区别
首先,我定了三个接口、一个结构和三个方法:
typeDeptModeAinterface{
Name()string
SetName(namestring)
}
typeDeptModeBinterface{
Relocate(buildingstring,flooruint8)
}
typeDeptstruct{
namestring
buildingstring
flooruint8
Keystring
}
func(selfDept)Name()string{
returnself.name
}
func(selfDept)SetName(namestring){
self.name=name
}
func(self*Dept)Relocate(buildingstring,flooruint8){
self.building=building
self.floor=floor
}
而后我写了一些测试代码:
dept1:=
Dept{
name:"MySohu",
building:"Internet",
floor:7}
switchv:=interface{}(dept1).(type){
caseDeptModeFull:
fmt.Printf("Thedept1isaDeptModeFull.\n")
caseDeptModeB:
fmt.Printf("Thedept1isaDeptModeB.\n")
caseDeptModeA:
fmt.Printf("Thedept1isaDeptModeA.\n")
default:
fmt.Printf("Thetypeofdept1is%v\n",v)
}
deptPtr1:=&dept1
if_,ok:=interface{}(deptPtr1).(DeptModeFull);ok{
fmt.Printf("ThedeptPtr1isaDeptModeFull.\n")
}
if_,ok:=interface{}(deptPtr1).(DeptModeA);ok{
fmt.Printf("ThedeptPtr1isaDeptModeA.\n")
}
if_,ok:=interface{}(deptPtr1).(DeptModeB);ok{
fmt.Printf("ThedeptPtr1isaDeptModeB.\n")
}
打印出的内容:
Thedept1isaDeptModeA. ThedeptPtr1isaDeptModeFull. ThedeptPtr1isaDeptModeA. ThedeptPtr1isaDeptModeB.
假设T是struct,那么Go里面遵循下面几个原则:
- T的方法集仅拥有TReceiver(方法中的接受者)方法。
- *T方法集则包含全部方法(T+*T)。
所以你上面的例子dept1应该是拥有方法:Name和SetName
而&dept1拥有方法:Name、SetName和Relocate
这个就是Go里面在设计方法的时候需要注意Receiver的类型
Go语言中结构体方法副本传参与指针传参的区别
我们来看个例子:
packagemain
import(
"fmt"
)
typeBstruct{
Namestring
}
func(bB)Test1(){
fmt.Printf("Test1addr:%p\n",&b)
fmt.Printf("Test1name:%s\n",b.Name)
b.Name="john"
}
func(b*B)Test2(){
fmt.Printf("Test2addr:%p\n",b)
fmt.Printf("Test2name:%s\n",b.Name)
b.Name="john"
}
funcmain(){
b:=B{}
b.Test1()
b.Test1()
b.Test2()
b.Test2()
}
执行后结果如下:
Test1addr:0xc42000e1e0 Test1name: Test1addr:0xc42000e1f0 Test1name: Test2addr:0xc42000e1d0 Test2name: Test2addr:0xc42000e1d0 Test2name:john
可以看到Test1中打印出b结构体的地址在变化,而Test2中没有变化,这说明每一次Test1的调用,都是传入的结构体b的一个副本(拷贝),当在Test1中对内部变量的任何改动,都将会失效(因为下一次访问的时候传入的是b结构体新的副本)。而Test2方法作为指针传参时,每一次传入的都是b结构体的指针,指向的是同一个结构体,因此地址没有变化,且对内部变量做改动时,都是改动的b结构体内容。
在Go语言中的这个差别可能是对OOP设计的一个坑,在Go语言中要想实现OOP的设计,在进行方法封装时,都采用Test2的写法。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。