暂无描述

response.go 466B

123456789101112131415161718192021222324252627
  1. package response
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. )
  6. var responseCode = 200
  7. //JSON :response json
  8. func JSON(w http.ResponseWriter, data interface{}) {
  9. js, err := json.Marshal(data)
  10. if err != nil {
  11. http.Error(w, err.Error(), http.StatusInternalServerError)
  12. return
  13. }
  14. w.Header().Set("Content-Type", "application/json")
  15. w.WriteHeader(responseCode)
  16. w.Write(js)
  17. }
  18. //SetCode :informa o código de resposta
  19. func SetCode(code int) {
  20. responseCode = code
  21. }