httputils_write_json_go16.go 376 B

12345678910111213141516
  1. // +build go1.6,!go1.7
  2. package httputils
  3. import (
  4. "encoding/json"
  5. "net/http"
  6. )
  7. // WriteJSON writes the value v to the http response stream as json with standard json encoding.
  8. func WriteJSON(w http.ResponseWriter, code int, v interface{}) error {
  9. w.Header().Set("Content-Type", "application/json")
  10. w.WriteHeader(code)
  11. enc := json.NewEncoder(w)
  12. return enc.Encode(v)
  13. }