I'm just starting with Go so I'm still not used to its patterns.
I have a web server that serves as a proxy to other remote services. I'm using mux
to map routes to handlers, the code it's using App Engine.
// imports ommited.
func init() {
m = mux.NewRouter()
m.HandleFunc("/ponies", listPonies)
m.HandleFunc("/rainbows", listRainbows)
http.Handle("/", m)
}
func listPonies(w http.ResponseWriter, r *http.Request) {
ponies, err := ponyService.getAll()
if err != nil {
w.write(err.Error())
return;
}
w.write(string(ponies))
}
func listRainbows(w http.ResponseWriter, r *http.Request) {
rainbows, err := rainbowService.getAll()
if err != nil {
w.write(err.Error())
return;
}
w.write(string(rainbows))
}
I would like to refactor the common code (error handling, converting to string and writing the response) into a single function.
My first attempt was simply defining a common function to call:
func handleErrorAndWriteResponse(w http.ResponseWriter, obj Stringer, err error) {
if err != nil {
w.write(err.Error())
return;
}
w.write(string(obj))
}
And call it like this
func listPonies(w http.ResponseWriter, r *http.Request) {
handleErrorAndWriteResponse(w, ponyService.getAll())
}
func listRainbows(w http.ResponseWriter, r *http.Request) {
handleErrorAndWriteResponse(w, rainbowService.getAll())
}
But
- It doesn't work. I get an
insufficient arguments
error. It probably has to do with mixing the multiple response values from the services that don't translate directly into arguments of the function called. - I don't like the idea of passing the error arguments around. Maybe that's fine, it just looks dirty to me. Then again, I don't know much about Go yet.
What's the "right way" (or the Go way) to do this?