gpt4 book ai didi

go - 方法接收者和参数有什么区别?

转载 作者:IT王子 更新时间:2023-10-29 00:37:55 24 4
gpt4 key购买 nike

查看下面显示的 Go 文档,我无法理解接收器和参数之间的区别:

 func (p *Page) save() error {
filename := p.Title + ".txt"
return ioutil.WriteFile(filename, p.Body, 0600)
}

This method's signature reads: This is a method named save that takes as its receiver p, a pointer to Page . It takes no parameters, and returns a value of type error.

最佳答案

接收器类似于 C# 中的 this:在 x.f(a, b, c) 中,接收器是 x 并且参数是 abc。执行函数时,参数引用参数的副本。接收者和参数之间的重要区别在于,当接收者是调用点的接口(interface)类型时,要调用的函数是动态确定的,而不是静态确定的。

关于go - 方法接收者和参数有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27159457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com