A Tour of Go Methods with pointer receivers
Methods can be associated with a named type or a pointer to a named type.
We just saw two Abs
methods. One on the *Vertex
pointer type and the other on the MyFloat
value type.
There are two reasons to use a pointer receiver. First, to avoid copying the value on each method call (more efficient if the value type is a large struct). Second, so that the method can modify the value that its receiver points to.
Try changing the declarations of the Abs
and Scale
methods to use Vertex
as the receiver, instead of *Vertex
.
The Scale
method has no effect when v
is a Vertex
. Scale
mutates v
. When v
is a value (non-pointer) type, the method sees a copy of the Vertex
and cannot mutate the original value.
Abs
works either way. It only reads v
. It doesn't matter whether it is reading the original value (through a pointer) or a copy of that value.
package main import (
"fmt"
"math"
) type Vertex struct {
X, Y float64
} func (v *Vertex) Scale(f float64) {
v.X = v.X * f
}
func (v *Vertex) Abs() float64{
return math.Sqrt(v.X*v.X + v.Y*v.Y)
} func main() {
v := &Vertex{, }
v.Scale()
fmt.Println(v, v.Abs())
}
package main import (
"fmt"
"math"
) type Vertex struct {
X, Y float64
} func (v Vertex) Scale(f float64) {
v.X = v.X * f
}
func (v Vertex) Abs() float64{
return math.Sqrt(v.X*v.X + v.Y*v.Y)
} func main() {
v := &Vertex{, }
v.Scale()
fmt.Println(v, v.Abs())
}
A Tour of Go Methods with pointer receivers的更多相关文章
- A Tour of Go Methods
Go does not have classes. However, you can define methods on struct types. The method receiver appea ...
- A Tour of Go Methods and Interfaces
The next group of slides covers methods and interfaces, the constructs that define objects and their ...
- A Tour of Go Methods continued
In fact, you can define a method on any type you define in your package, not just structs. You canno ...
- Golang 学习资料
资料 1.How to Write Go Code https://golang.org/doc/code.html 2.A Tour of Go https://tour.golang.org/li ...
- golang开发:类库篇(五)go测试工具goconvey的使用
为什么要使用goconvey测试程序 goconvey 集成go test,go test 无缝接入.管理运行测试用例,而且提供了丰富的函数断言.非常友好的WEB界面,直观的查看测试结果. 如果没有g ...
- golang之method
method Go does not have classes. However, you can define methods on types. package main import ( &qu ...
- Go Methods and Interfaces
[Go Methods and Interfaces] 1.Go does not have classes. However, you can define methods on struct ty ...
- 【GoLang】50 个 Go 开发者常犯的错误
1. { 换行: Opening Brace Can't Be Placed on a Separate Line 2. 定义未使用的变量: Unused Variables 2. import ...
- [转]50 Shades of Go: Traps, Gotchas, and Common Mistakes for New Golang Devs
http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/ 50 Shades of Go: Traps, Gotc ...
随机推荐
- DJANGO输出HIGHCHARTS数据的样例
XXX,DJANGO ORM里确实有很深的水,需要慢慢理解.. 比如: 获取指定时间段的数据: app.deployversion_set.filter(add_date__range=(date_s ...
- jmeter 启用gzip压缩——解决测试中web服务器上行流量过大的问题
最近测了几个前端的项目,发现它们都有一个共同点:应用所在服务器的网卡上行(trans)非常大——经常是 117 MB/S,这已经逼近了千兆网卡的极限了.下面记录下排查和解决过程: 一. jmeter ...
- java thread类和runable
java thread 类其实和其他类是一样的.有自己的属性和方法.以及有一个重写的run方法 不同的是:必须重写run()方法. run()方法是线程thread启动后cpu调用运行的程序代码. r ...
- WebLoigc的配置(生产模式与开发模式)
1.Weblogic两种模式的切换1).生产模式--->开发模式将domain路径下的bin\setDomainEnv.cmd文件中set PRODUCTION_MODE=true改为set P ...
- tomcat集群部署
1.apache只有处理静态事物的能力, 而tomcat的强项就是处理动态的请求 2.由apache作为入口,如果是请求静态页面或者是静态文件,由apache直接提供,如果是请求动态页面,则让apac ...
- 每个QWidget都有contentsMargins函数,善用QMargins
m_pSearchLineEdit = new QLineEdit(); QPushButton *pSearchButton = new QPushButton(this); pSearchButt ...
- 使用Mysql命令一次性备份多个数据库(所有数据库)
通过CMD命令窗口 1.找到Mysql的安装目录\mysql\BIN目录 2. 输入mysqldump --all-databases -h127.0.0.1 -uroot -p123456 > ...
- E-BOM和M-BOM的区别
简单一点,ENG BOM一般用于试产,正式BOM一般用于量产:ENG BOM是FOR RD设计用的.即TEMP档.并非正式区的.一般的电子零件类的企业都会用到ENG BOM.在EBS中,ENG BOM ...
- bzoj1858: [Scoi2010]序列操作
lazy-tag线段树. #include<cstdio> #include<algorithm> #include<cstring> using namespac ...
- (转载)内联函数inline和宏定义
(转载)http://blog.csdn.net/chdhust/article/details/8036233 内联函数inline和宏定义 内联函数的优越性: 一:inline定义的类的内联函 ...