调用方法表达式时没有足够的参数 [英] not enough arguments in call to method expression
问题描述
在学习的过程中,我遇到以下错误:
While learning go I came to following error:
prog.go:18: not enough arguments in call to method expression JSONParser.Parse
在我的测试程序中( https://play.golang.org/p/PW9SF4c9q8 ):
in my test program (https://play.golang.org/p/PW9SF4c9q8):
package main
type Schema struct {
}
type JSONParser struct {
}
func (jsonParser JSONParser) Parse(toParse []byte) ([]Schema, int) {
var schema []Schema
// whatever parsing logic
return schema, 0
}
func main() {
var in []byte
actual, err2 := JSONParser.Parse(in)
}
任何人都愿意帮助我继续前进?
Anyone willing to help me to move on here?
推荐答案
不幸的是,你的错误有点误导。问题在于它是一个实例方法,你将它称为它是一个包范围的方法。
Your error unfortunately is somewhat misleading. The issue is that it is an instance method and you're calling it as if it's a method at the packages scope.
你需要类似这样的东西;
You need something like this;
func main() {
var in []byte
jp := JSONParser{}
actual, err2 := jp.Parse(in)
}
我猜错误是这样的措辞,因为一个接收器(函数名左手站点parens中的东西)被处理像传递给后台函数的任何其他参数。
I'm guessing the error is worded like that because a receiver (thing in parens on the left hand site of function name) is handled like any other argument being passed to a function in the background.
如果你想调用你的方法,这个定义只是 func Parse(toParse [] byte)([] Schema,int)
,如果它在一个名为 JSONParser
那么这将是正确的语法。如果它在你的例子中定义在同一个包中,那么你可以称它为 Parse(in)
If you wanted to call your method like that the definition would just be func Parse(toParse []byte) ([]Schema, int)
and if it were in a package called JSONParser
then that would be the correct syntax. If it were defined in the same package as in your example you would just call it like Parse(in)
这篇关于调用方法表达式时没有足够的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!