dongsi7759 2016-07-29 19:43 采纳率: 100%
浏览 590
已采纳

调用方法表达式的参数不足

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?

  • 写回答

1条回答 默认 最新

  • duanbei3747 2016-07-29 19:45
    关注

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建