发布于 2018-02-08 11:58:07 | 142 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的GO语言教程,程序狗速度看过来!

Go语言

Go是一种新的语言,一种并发的、带垃圾回收的、快速编译的语言。Go是谷歌2009年发布的第二款编程语言。2009年7月份,谷歌曾发布了Simple语言,它是用来开发Android应用的一种BASIC语言。


这篇文章主要给大家介绍了关于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的写法。

总结

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务