not enough arguments in call to method expression

后端 未结 1 1515
-上瘾入骨i
-上瘾入骨i 2021-02-19 01:03

While learning go I came to following error:

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

in my test progra

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-19 01:29

    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)

    0 讨论(0)
提交回复
热议问题