2016-04-14 30 views
0

鉴于以下代码:使用地图[字符串]接口{}:

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) 

} 

的想法是能够发送一个未知量的一个块ID1 => VAL1,ID2 => val2的...到路由。

它给了我这个错误:

missing type in composite literal

+0

它是一个错字,你的'Action'应是“结果”?或者它是你忘记在结构中定义的字段? – nevets

回答

4

你应该初始化它是这样的:

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

精简编译:http://play.golang.org/p/bXVOwIhLlg

相关问题