Peter Butkovic
Peter Butkovic

Reputation: 12189

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 

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?

Upvotes: 23

Views: 26257

Answers (1)

evanmcdonnal
evanmcdonnal

Reputation: 48154

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)
}

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.

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)

Upvotes: 33

Related Questions