Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GO lang syntax error: unexpected name, expecting )

Tags:

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
}

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.

like image 610
Ahmed_Ali Avatar asked Mar 22 '16 11:03

Ahmed_Ali


3 Answers

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)
like image 51
Syed Muhammad BalighurRahman Avatar answered Oct 14 '22 01:10

Syed Muhammad BalighurRahman


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

like image 36
cricrishi Avatar answered Oct 14 '22 01:10

cricrishi


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
}
like image 1
Nick Brady Avatar answered Oct 14 '22 00:10

Nick Brady