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 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{3, 4}
    v.Scale(5)
    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{3, 4}
    v.Scale(5)
    fmt.Println(v, v.Abs())
}
时间: 2024-07-29 07:13:35

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 appears in its own argument list between the func keyword and the method name. package main import ( "fmt" "math" ) type Vertex struct { X,

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 cannot define a method on a type from another package, or on a basic type. package main import ( "fmt" "math" ) type MyFloat float64 func (f

Go Methods and Interfaces

[Go Methods and Interfaces] 1.Go does not have classes. However, you can define methods on struct types. The method receiver appears in its own argument list between the func keyword and the method name. 2.You can declare a method on any type that is

[转]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, Gotchas, and Common Mistakes for New Golang Devs Go is a simple and fun language, but, like any other language, it has a few gotchas... Many of those gotc

golang中container/heap包源码分析

学习golang难免需要分析源码包中一些实现,下面就来说说container/heap包的源码 heap的实现使用到了小根堆,下面先对堆做个简单说明 1. 堆概念 堆是一种经过排序的完全二叉树,其中任一非终端节点的数据值均不大于(或不小于)其左孩子和右孩子节点的值. 最大堆和最小堆是二叉堆的两种形式. 最大堆:根结点的键值是所有堆结点键值中最大者. 最小堆:根结点的键值是所有堆结点键值中最小者. 2. heap 树的最小元素在根部,为index 0. heap包对任意实现了heap接口的类型提供

golang基础练习(一)

//遍历map package main import "fmt" func main() { x := make(map[string]int) x["zhangsan"] = 3 x["lisi"] = 4 x["wangwu"] = 5 //#丢弃值 for i,_ := range x { fmt.Println(i) } } //匿名函数 package main import "fmt" fun

for循环的两种写法

教程 (https://tour.golang.org/methods/21) 里的 for 是这样写的: 其中 for 语句可以改写如下: for n, err := r.Read(b); err != io.EOF; n, err = r.Read(b) { fmt.Printf("n = %v err = %v b = %v\n", n, err, b) fmt.Printf("b[:n] = %q\n", b[:n]) } (当然,golang 里的 for

GO学习笔记 - 函数名前面是否有输入参数肯定是不一样的!!

在刚接触GO语言时候,我相信你也会有这种困惑,为什么有的函数名前面有输入参数,而一些却没有,它们是否有差别?确实有差别,没有输入参数,是一般的函数:有输入参数,是结构的方法,输入参数叫做"方法接收者"!GO语言没有类,方法都定义在结构上了!! 官方教程: 函        数:https://tour.go-zh.org/basics/4 结构体方法:https://tour.go-zh.org/methods/1 实例代码: main.go : 引入了"sunylat/de

golang初学之接口---image

来自golang tour 练习 https://tour.go-zh.org/methods/16 package main import "golang.org/x/tour/pic" import "image" import "image/color" type Image struct{ x, y, width, height int } func (im Image) ColorModel() color.Model { return