Skip to content

Instantly share code, notes, and snippets.

@s1s1ty s1s1ty/go-sql-6.go
Created Jul 18, 2018

Embed
What would you like to do?
6th part
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
func catch(err error) {
if err != nil {
panic(err)
}
}
// respondwithError return error message
func respondWithError(w http.ResponseWriter, code int, msg string) {
respondwithJSON(w, code, map[string]string{"message": msg})
}
// respondwithJSON write json response format
func respondwithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
fmt.Println(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}
// Logger return log message
func Logger() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println(time.Now(), r.Method, r.URL)
router.ServeHTTP(w, r) // dispatch the request
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.