Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using map[string]interface{} :

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

like image 263
abdel Avatar asked Dec 05 '22 18:12

abdel


1 Answers

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

like image 106
william.taylor.09 Avatar answered Dec 28 '22 13:12

william.taylor.09