Im attempting to set a default header in my Golang web api. I am using the net/http router that comes with Go. I can find many examples of how to do this :
Is it there a way in which I can set a default header for all responses using net/http?
Create a wrapper function. Something like:
package main
import "net/http"
func SetDefaultHeaders(handler func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// Set default headers
handler(w, r)
}
}
func main() {
http.HandleFunc("/", SetDefaultHeaders(func(w http.ResponseWriter, r *http.Request) {
// ...
}))
}