亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

go中的任意JSON數據結構

go中的任意JSON數據結構

Go
UYOU 2021-06-10 14:04:19
我正在構建一個 http api 并且我的每個處理程序都返回 JSON 數據,因此我構建了一個包裝器函數來處理 JSON 編組和 http 響應(我已經包含了包裝器中的相關部分以及示例處理程序之一以下)。傳遞任意嵌套結構的最佳方法是什么(結構還包含任意類型/數量的字段)?,F在我已經確定了一個帶有字符串鍵和 interface{} 值的映射。這有效,但這是最慣用的方法嗎?result := make(map[string]interface{})customerList(httpRequest, &result)j, err := json.Marshal(result)if err != nil {    log.Println(err)    errs := `{"error": "json.Marshal failed"}`    w.Write([]byte(errs))    return}w.Write(j)func customerList(req *http.Request, result *map[string]interface{}) {    data, err := database.RecentFiftyCustomers()    if err != nil {        (*result)["error"] = stringifyErr(err, "customerList()")        return    }    (*result)["customers"] = data//data is a slice of arbitrarily nested structs}
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 213 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號