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 VertexScale 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的更多相关文章

  1. A Tour of Go Methods

    Go does not have classes. However, you can define methods on struct types. The method receiver appea ...

  2. A Tour of Go Methods and Interfaces

    The next group of slides covers methods and interfaces, the constructs that define objects and their ...

  3. 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 ...

  4. Golang 学习资料

    资料 1.How to Write Go Code https://golang.org/doc/code.html 2.A Tour of Go https://tour.golang.org/li ...

  5. golang开发:类库篇(五)go测试工具goconvey的使用

    为什么要使用goconvey测试程序 goconvey 集成go test,go test 无缝接入.管理运行测试用例,而且提供了丰富的函数断言.非常友好的WEB界面,直观的查看测试结果. 如果没有g ...

  6. golang之method

    method Go does not have classes. However, you can define methods on types. package main import ( &qu ...

  7. Go Methods and Interfaces

    [Go Methods and Interfaces] 1.Go does not have classes. However, you can define methods on struct ty ...

  8. 【GoLang】50 个 Go 开发者常犯的错误

    1. { 换行:   Opening Brace Can't Be Placed on a Separate Line 2. 定义未使用的变量:  Unused Variables 2. import ...

  9. [转]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 ...

随机推荐

  1. PJSIP在windows(xp或者win7)下的编译,编译工具是vs2008,PJSIP版本2.3

    PJSIP是一个开源的SIP协议库,它实现了SIP.SDP.RTP.STUN.TURN和ICE.PJSIP作为基于SIP的一个多媒体通信框架提供了非常清晰的API,以及NAT穿越的功能.PJSIP具有 ...

  2. 手动添加 memcached.jar包

    由于目前java memcached client没有官方的maven repository可供使用,因此使用时需要手动将其安装到本地repository. java memcached client ...

  3. Octave下载

    发福利啦,今天下了半天Octave都没下载下来,最后让一个香港的同学帮忙下好传过来的....放到网盘里造福大家 GNU_Octave_3.8.0-6.dmg 链接: http://pan.baidu. ...

  4. 函数buf_LRU_get_free_only

    /******************************************************************//** Returns a free block from th ...

  5. CodeForces Round #287 Div.2

    A. Amr and Music (贪心) 水题,没能秒切,略尴尬. #include <cstdio> #include <algorithm> using namespac ...

  6. Idea无法DEBUG的问题

    最近对web工程进行debug,突然发现无法进入断点了,原来以为是maven的问题,后来发现是tomcat环境变量导致的. 使用tomcat时经常碰到内存不足的情况,我们会对catalina.bat类 ...

  7. ExecuteStoreQuery

    using (var webdb = new kyj_NewHouseDBEntities()) { string sql = "select * from developer where ...

  8. CSS sprite 圆角——源代码

    所需图片:                                                                                         corner ...

  9. 一滴一点vim(学习+备忘)

    普通模式: h j k l 分别是左下上右方式移动: :w 保存修改 :q 推出 :wq 保存修改并退出 :q! 放弃修改强制推出 x 删除光标所在位置字符 i 在光标所以位置插入字符 删除类命令: ...

  10. ubuntu设置ip和dns

      装完ubuntu 第一件事情就是连上网,换个源,进行更新操作,但前提条件是要配好ip和dns.   下面把自己配置的过程记录下来,权且当作一份备份,以便不时之需.   一.配置ip      ub ...