GO语言语法错误:意外的名称,期望 )

huangapple go评论84阅读模式
英文:

GO lang syntax error: unexpected name, expecting )

问题

我最近开始学习Go语言。我花了几个小时,但是无法弄清楚问题出在哪里。

以下是我的代码:

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)
	
    b, err := json.Marshal(response)
    if err != nil {
        http.Error(w, "Internal Error", http.StatusInternalServerError)
        return
    }
    fmt.Fprintf(w, string(b[:]))
}

出现了以下错误:语法错误:意外的名称,期望是")"。这可能很简单,但是由于我在Go语言方面的知识有限,我无法弄清楚。

英文:

I have started learning Go lang recently.I have spend couple of hours but can't figure out what's wrong with this.

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
}

// compile time error on below line
response := models.UserPrefer(cc, userID int64, key string, value string)

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.

答案1

得分: 10

你在调用方法时传递了类型

使用

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

而不是

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

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)

答案2

得分: 2

在调用函数时,只需传递参数即可,无需传递参数的类型。

英文:

While calling a function just pass the parameter. You do not need to pass the type of parameter.

答案3

得分: 1

我在实例化类型时忘记加上冒号时,遇到了一个非常类似的错误。我创建了一个最小的示例来说明。

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

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

r := Rock{
	RockType:	"Sedimentary", // 缺少了“:”,在上面的go play链接中有
}
英文:

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 }

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
}

答案4

得分: 1

我之前遇到这个错误是因为我把一个保留字作为方法的参数名使用了。

引发错误的代码片段:

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

}

修复这个问题的代码片段:

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

}
英文:

I was getting this error because I was using a reserved keyword as an argument name for a method.

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) {

}

huangapple
  • 本文由 发表于 2016年3月22日 19:18:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/36152999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定