我最近开始学习 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 语言的了解有限,我无法弄清楚。
最佳答案
你在调用方法时传递类型
使用
response :=models.UserPrefer(cc, userID, key, value)
代替
response :=models.UserPrefer(cc, userID int64, key string, value string)
关于GO lang 语法错误 : unexpected name, 期待),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36152999/