GO API: Utill
constant.go
constant.go
package utils
const PORT string = "9090"
const COCKROACHDB_USERNAME_ENV string = "username"
const COCKROACHDB_DB_ENV string = "students"
const COCKROACHDB_HOST_ENV string = "localhost"
const COCKROACHDB_PORT_ENV string = "26257"
rest_util.go package utils
import (
"encoding/json"
"net/http"
)
func WriteHttpResponse(w http.ResponseWriter, v interface{}, err error) {
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
js, err := json.Marshal(v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(js)
}
func WriteEmptyHttpResponse(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNoContent)
}
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Comments
Post a Comment