Not enough arguments in method call

During training, I came up with the following error:

prog.go:18: not enough arguments in call to method expression JSONParser.Parse 

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 want to help me move here?

+6
source share
1 answer

Your mistake, unfortunately, is somewhat misleading. The problem is that this is an instance method, and you call it as if it were a package method.

You need something like this:

 func main() { var in []byte jp := JSONParser{} actual, err2 := jp.Parse(in) } 

I assume that the error is formulated as follows, because the receiver (thing in parens on the left site of the function name) is treated like any other argument passed to the function in the background.

If you want to call your method this way, the definition will be just func Parse(toParse []byte) ([]Schema, int) , and if it were in a package called JSONParser , that would be the correct syntax. If it was defined in the same package as in your example, you simply name it as Parse(in)

+6
source

All Articles