2016-04-14 27 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

相關問題