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

栏目: 编程语言 · Go · 发布时间: 7年前

内容简介:这篇文章主要给大家介绍了关于Go语言中结构体方法副本传参与指针传参的区别的相关资料,文中先对GO语言结构体方法跟结构体指针方法的区别进行了一些简单的介绍,来帮助大家理解学习,需要的朋友可以参考下。

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的写法。

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


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Pro Django

Pro Django

Marty Alchin / Apress / 2008-11-24 / USD 49.99

Django is the leading Python web application development framework. Learn how to leverage the Django web framework to its full potential in this advanced tutorial and reference. Endorsed by Django, Pr......一起来看看 《Pro Django》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具