func (app *application) writeJSON(w http.ResponseWriter, status int, data envelope, headers http.Header) error { js, err := json.MarshalIndent(data, "", " ") if err != nil { return err } // Append a newline to make it easier to view in terminal applications. js = append(js, '\n') // At this point, we know that we won't encounter any more errors before writing the response, // so it's safe to add any headers that we want to include. We loop through the header map and // add each header to the http.ResponseWriter header map. Note that it's OK if the provided // header map is nil. Go doesn't throw an error if you try to range over a nil map. for key, value := range headers { w.Header()[key] = value // w.Header().Set(key, value) } // iterator version // maps.Insert(w.Header(), maps.All(headers)) // Add the "Content-Type: application/json" header, then write the status code and JSON // response. w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) w.Write(js) return nil }
Using json.Encoder
func (app *application) exampleHandler(w http.ResponseWriter, r *http.Request) { data := map[string]string{ "hello": "world", }
// Set the "Content-Type: application/json" header on the response. w.Header().Set("Content-Type", "application/json")
// Use the json.NewEncoder() function to initialize a json.Encoder instance that // writes to the http.ResponseWriter. Then we call its Encode() method, passing in // the data that we want to encode to JSON (which in this case is the map above). If // the data can be successfully encoded to JSON, it will then be written to our // http.ResponseWriter. err := json.NewEncoder(w).Encode(data) if err != nil { app.logger.Error(err.Error()) http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError) } }
Performance of json.Encoder and json.Marshal
Talking of speed, you might be wondering if there’s any performance difference between using json.Encoder and json.Marshal() . The short answer to that is yes… but the difference is small and in most cases you should not worry about it.
Additional JSON encoding nuances
Encoding things to JSON in Go is mostly quite intuitive. But there are a handful of behaviors which might either catch you out or surprise you when you first encounter them.
We’ve already mentioned a couple of these in this chapter (in particular — map entries being sorted alphabetically and byte slices being base-64 encoded), but I’ve included a full list in this appendix.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2023-11-14 Protobuf - Protocol Buffer Compiler
2023-11-14 Protobuf - FIELD NUMBERS