Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not enough arguments in call to method expression

Tags:

go

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?

like image 477
Peter Butkovic Avatar asked Jul 29 '16 19:07

Peter Butkovic


1 Answers

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)

like image 114
evanmcdonnal Avatar answered Sep 20 '22 19:09

evanmcdonnal