4

Given the following code:

type Message struct {
    Params map[string]interface{} `json:"parameters"`
    Result interface{}            `json:"result"`
}

func (h Handler) Product(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {

    msg := &Message{
        Action: "get_products",
        Params: {
            "id1": val1,
            "id2": val2,
        },
    }
     h.route(msg)

}

The idea is to be able to send a block of an unknown amount id1 => val1, id2 =>val2 ... to h.route.

it gives me this error:

missing type in composite literal

1
  • is it a typo that your Action should be Result? or it's a field you forgot to define in the struct? Commented Apr 14, 2016 at 15:21

1 Answer 1

15

You should initialize it like this:

func (h Handler) Product(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
    msg := &Message{
        Action: "get_products",
        Params: map[string]interface{}{
            "id1": val1,
            "id2": val2,
        },
    }
    h.route(msg)
}

Stripped down to compile: http://play.golang.org/p/bXVOwIhLlg

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.