GO lang 语法错误:意外名称,期待 )

我最近开始学习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 语言的了解有限,我无法弄清楚。


MM们
浏览 204回答 3
3回答

潇潇雨雨

您在调用方法时传递类型利用response :=models.UserPrefer(cc, userID, key, value)代替response :=models.UserPrefer(cc, userID int64, key string, value string)

慕沐林林

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

慕盖茨4494581

我收到此错误是因为我使用reserved关键字作为方法的参数名称。引发此错误的代码片段:func setCustomTypeData(set bson.M, type string) {}解决此问题的代码片段:func setCustomTypeData(set bson.M, customManagedType string) {}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go