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 ...
随机推荐
- POJ 3761 Bubble Sort(乘方取模)
点我看题目 题意 : 冒泡排序的原理众所周知,需要扫描很多遍.而现在是求1到n的各种排列中,需要扫描k遍就变为有序的数列的个数,结果模20100713,当然了,只要数列有序就扫描结束,不需要像真正的冒 ...
- 每次都觉得很神奇的JS
匿名,函数对象... var staff = [ {name: 'abruzzi', age: 24}, {name: 'bajmine', age: 26}, {name: 'chris', age ...
- poj 2454 Jersey Politics 随机化
随机化算法+贪心! 将3*k排序后分成3分,将第二第三份的和分别加起来,让和与500*k比较,都大于则输出,否则,随机生成2个数,在第二第三份中交换! 代码如下: #include<iostre ...
- java内存模型 年轻代/年老代 持久区
jvm中的年轻代 老年代 持久代 gc 虚拟机中的共划分为三个代:年轻代(Young Generation).老年代(Old Generation)和持久代(Permanent Generatio ...
- HeadFirst设计模式之适配器模式
一. 1. 2.The Adapter Pattern converts the interface of a class into another interface the clients exp ...
- Android:自定义适配器
无论是ArrayAdapter还是SimpleAdapter都继承了BaseAdapter,自定义适配器同样继承BaseAdapter 实例:Gallery实现图片浏览器 <?xml versi ...
- poj2823Sliding Window(线段树求最值)
链接 裸线段树 这题时间卡的挺棒 #include <iostream> #include<cstdio> #include<cstring> #include&l ...
- My97 DatePicker使用之自定义事件
参考网站:http://www.my97.net/dp/demo/resource/2.5.asp 自定义事件 如果你需要做一些附加的操作,你也不必担心,日期控件自带的自定义事件可以满足你的需求.此外 ...
- tap,touch,touchstart,事件与click事件的区别
根据源码所见, 移动端为了将将单击事件更加灵敏,所以现在的JQM,ST...框架都将JS单击事件封装成tap,或者touch或者touchstart事件, 其实现本质是将click触发多次,以打成移动 ...
- Highcharts20151130
$(function () { $('#container').highcharts({ chart: { type: 'spline' // 图的类型 }, title: { text: null ...