英文:
Parameter before the function name in Go?
问题
我看到一些Go函数的定义如下:
type poly struct {
coeffs [256]uint16
}
func (p *poly) reset() {
for i := range p.coeffs {
p.coeffs[i] = 0
}
}
之后你可以这样调用它:
var p poly
p.reset()
在我所了解的其他编程语言中,我没有见过这种写法。p *poly
在 reset 函数中的作用是什么?它似乎像一个函数参数,但是写在函数名之前。你能解释一下吗?
英文:
I have seen some Go functions defined like this:
type poly struct {
coeffs [256]uint16
}
func (p *poly) reset() {
for i := range p.coeffs {
p.coeffs[i] = 0
}
}
Which you can later call as:
var p poly
p.reset()
I haven't seen this in other programming languages that I know. What's the purpose of p *poly
in the reset function? It seems to be like a function parameter but written before the function name. Any clarification for it?
答案1
得分: 37
这意味着reset()
是*poly
上的一个方法。
英文:
It means that reset()
is a method on *poly
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论