使用map [string] interface {}:

abd*_*del 3 dictionary interface go

给出以下代码:

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)

}
Run Code Online (Sandbox Code Playgroud)

我们的想法是能够将未知量的块id1 => val1,id2 => val2 ...发送到h.route.

它给了我这个错误:

复合文字中缺少类型

wil*_*.09 11

你应该像这样初始化它:

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)
}
Run Code Online (Sandbox Code Playgroud)

剥离编译:http://play.golang.org/p/bXVOwIhLlg