package response import ( "encoding/json" "fmt" "log" "net/http" ) // JSON makes a JSON response func JSON(writer http.ResponseWriter, status int, data interface{}) { jsonData, err := json.Marshal(data) if err != nil { log.Println("JSON Marshal failed: ", err.Error()) writer.Header().Set("Content-Type", "text/plain; charset=utf-8") writer.WriteHeader(503) fmt.Fprint(writer, "JSON marshalling failed:", err.Error()) return } writer.Header().Set("Content-Type", "application/json") writer.WriteHeader(status) writer.Write(jsonData) }