package main
import (
"fmt"
)
type vector struct {
x int
y int
}
func (u vector) add(v vector) vector {
fmt.Println("received: ", u)
u.x += v.x
u.y += v.y
return u
}
func main() {
vecA := vector{x: 5, y: 10}
vecB := vector{x: 6, y: 7}
fp := vecA.add // 1
vecA = fp(vecB) // 2
fmt.Println(vecA)
vecA = fp(vecB) // 3
fmt.Println(vecA)
}
/*
Output:
received: {5 10}
{11 17}
received: {5 10}
{11 17}
*/
在标记1fp处,我使用add函数声明并初始化,vecA用作接收器。在标记2处,我更改了 的值vecA。现在在3处,如果我们扩展语句:fp(vecA),它变成:vecA.add(vecB)。现在我认为它应该add使用“已更改” vecA(在标记2vecA处更改)调用函数,而不是(在标记1处更改)的旧值,而是add使用“旧” vecA(在标记1处)调用函数,这从输出。为什么?
虽然我找到了一种使用新的方法,vecA如下所示:
package main
import (
"fmt"
)
type vector struct {
x int
y int
}
func (u *vector) add(v vector) {
fmt.Println("received: ", *u)
u.x += v.x
u.y += v.y
}
func main() {
vecA := &vector{x: 5, y: 10}
vecB := vector{x: 6, y: 7}
fp := vecA.add // 1
fp(vecB) // 2
fmt.Println(*vecA)
fp(vecB) // 3
fmt.Println(*vecA)
}
/*
Output:
received: {5 10}
{11 17}
received: {11 17}
{17 24}
*/
白板的微信
白猪掌柜的
largeQ
随时随地看视频慕课网APP
相关分类