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 ...
随机推荐
- HDU1432+几何
题意:给N个点,求最多有多少个点在同一直线上 方法一:求出所有能形成的直线,两两比较,统计最多有多少条重合. #include<stdio.h> #include<stdlib.h& ...
- http://nxlhero.blog.51cto.com/962631/1666250?plg_nld=1&plg_uin=1&plg_auth=1&plg_nld=1&plg_usr=1&plg_vkey=1&plg_dev=1
http://nxlhero.blog.51cto.com/962631/1666250?plg_nld=1&plg_uin=1&plg_auth=1&plg_nld=1&am ...
- 【零基础学习iOS开发】【02-C语言】11-函数的声明和定义
在上一讲中,简单介绍了函数的定义和使用,只要你想完成一个新功能,首先想到的应该是定义一个新的函数来完成这个功能.这讲继续介绍函数的其他用法和注意事项. 一.函数的声明 1.在C语言中,函数的定义顺序是 ...
- xcode 把cocos2d-x 以源码的形式包含进自己的项目适合, 性能分析问题的错误
性能分析:出现如下错误: xcode profile Variable has incomplete type class “CC_DLL” 解决办法:在 xcode的Build Setting ...
- mybatis传入map参数parameterType
基本数据类型:包含int,String,Date等.基本数据类型作为传参,只能传入一个.通过#{参数名} 即可获取传入的值 复杂数据类型:包含JAVA实体类.Map.通过#{属性名}或#{map的Ke ...
- 初始化D3D设备
bool initD3D(HWND hWnd) { // 主要目的是获取设备,为调用下面的函数做很多准备. // 比如 获取IDirect3D9 ,获取支持的顶点处理,填充后备缓冲相关参数等. // ...
- 【转】五种开源协议的比较(BSD, Apache, GPL, LGPL, MIT)
当 Adobe.Microsoft.Sun 等一系列巨头开始表现出对”开源”的青睐时,”开源”的时代即将到来! 现今存在的开源协议很多,而经过 Open Source Initiative 组织通过批 ...
- 通过dbms_xplan.display_cursor识别低效的执行计划
dbms_xplan.display_cursor定义: function display_cursor(sql_id varchar2 default null, ...
- dom4j 的小小测试
@Test public void gogo() throws IOException{ InputStream in = this.getClass().getClassLoader() .getR ...
- C#中种常用的计时器
1.System.Timers.Timer和System.Windows.Forms.Timer,它的最低识别为1/18s. 2.timeGetTime,他的最低识别能达到5ms. 3.System. ...