【问题标题】:GO lang syntax error: unexpected name, expecting )GO lang 语法错误:意外名称,期待)
【发布时间】:2020-11-08 13:33:34
【问题描述】:

我最近开始学习 Go lang。我花了几个小时但不知道这有什么问题。

这是我的代码:

func preference(cc *core.ComponentContext, w http.ResponseWriter, req *http.Request){

userID, err := core.PostParam(req, "user_id")
key, err := core.PostParam(req, "key")
value, err := core.PostParam(req, "value")  
if err != nil {
    cc.Error("Error reading the user id:", err.Error())
    msg := fmt.Sprintf("user_id: %s", err.Error())
    http.Error(w, msg, http.StatusBadRequest)
    return
}

response :=models.UserPrefer(cc, userID int64, key string, value string) --> compile time error

b, err := json.Marshal(response)
if err != nil {
    http.Error(w, "Internal Error", http.StatusInternalServerError)
    return
}
fmt.Fprintf(w, string(b[:]))

}

以下错误是抛出语法错误:意外名称,期待) 这可能很简单,但由于我对 Go 语言的了解有限,我无法弄清楚。

【问题讨论】:

  • 为什么在对UserPrefer的调用中有int64string这两个类型名称?
  • 在哪一行抛出错误?
  • @AjPennster。在代码中我提到了编译时错误。有什么不赞成投票的理由吗?
  • 不要编辑您的问题所涉及的错误。如果其他人读到这里,答案和 cmets 将不再有意义。

标签: go


【解决方案1】:

调用方法时传递类型

使用

response :=models.UserPrefer(cc, userID, key, value)

而不是

response :=models.UserPrefer(cc, userID int64, key string, value string)

【讨论】:

    【解决方案2】:

    调用函数时只需传递参数。您不需要传递参数的类型。

    【讨论】:

      【解决方案3】:

      当我在实例化类型时忘记输入冒号时,我得到了一个与此非常相似的错误。创建了一个最小示例来说明。

      prog.go:11:12:语法错误:意外的文字“Sedimentary”,需要逗号或}

      https://play.golang.org/p/QKmcOHnsF7C

      在这个例子中,我只需要在属性名后面加一个冒号。

      r := Rock{
          RockType:   "Sedimentary", // the ":" was missing, and is in the go play link above
      }
      

      【讨论】:

        【解决方案4】:

        我收到此错误是因为我使用 reserved 关键字作为方法的参数名称。

        引发此错误的代码 sn-p:

        func setCustomTypeData(set bson.M, type string) {
        
        }
        

        修复此问题的代码 sn-p:

        func setCustomTypeData(set bson.M, customManagedType string) {
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-09-12
          • 2016-05-16
          • 2019-12-06
          • 2013-11-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-26
          相关资源
          最近更新 更多