10

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.

4
  • 1
    Why do you have the type names int64 and string in the call to UserPrefer? Commented Mar 22, 2016 at 11:25
  • At what line is the error thrown? Commented Mar 22, 2016 at 11:32
  • @AjPennster. in code i have mention compile time error. any reason of down voting ? Commented Mar 22, 2016 at 11:49
  • 3
    Don't edit out the errors that your question is about. If someone else reads this, the answers and comments are no longer going to make sense. Commented Mar 22, 2016 at 11:57

4 Answers 4

11

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)
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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
}

Comments

1

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

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.