简体   繁体   English

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

[英]GO lang syntax error: unexpected name, expecting )

I have started learning Go lang recently.I have spend couple of hours but can't figure out what's wrong with this.我最近开始学习Go lang。我花了几个小时,但不知道这有什么问题。

Here is my code:这是我的代码:

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[:]))

} }

Following error is throw syntax error: unexpected name, expecting ) it's probably simple, but with my limited knowledge in Go lang I can't figure out.以下错误是抛出语法错误:意外名称,期望)这可能很简单,但由于我对 Go 语言的了解有限,我无法弄清楚。

You are passing types when calling methods您在调用方法时传递类型

Use使用

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

instead of而不是

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

While calling a function just pass the parameter.调用函数时只传递参数。 You do not need to pass the type of parameter.您不需要传递参数的类型。

I got an error very similar to this when I forgot to put in a colon while instantiating a type.当我在实例化类型时忘记输入冒号时,我得到了一个与此非常相似的错误。 Created a minimum example to illustrate.创建了一个最小的例子来说明。

prog.go:11:12: syntax error: unexpected literal "Sedimentary", expecting comma or } prog.go:11:12: 语法错误:意外文字“Sedimentary”,需要逗号或 }

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

In this example, I just needed to add a colon after the attribute name.在这个例子中,我只需要在属性名称后添加一个冒号。

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

I was getting this error because I was using a reserved keyword as an argument name for a method.我收到此错误是因为我使用reserved关键字作为方法的参数名称。

Code snippet that was throwing this error:引发此错误的代码片段:

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

}

Code snippet that fixed this issue:解决此问题的代码片段:

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM