httpreturnchecker
is a static analysis tool that checks if you forget to
correctly use a return
after writing to the w
variable - http.ResponseWriter
Why this?
Read this article http://lanre.wtf/blog/2024/08/25/http-handler-errors-golang
go install github.com/adelowo/httpreturnchecker
go vet -vettool=$(which httpreturnchecker) path/to/file.go
It only handles scenarios like this:
fmt.Fprint
family of functionsio.Copy
w.Write()
render.Render
using github.com/go-chi/render
// OK - return here
func handler1(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
return
}
// OK - last statement
func handler2(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
}
// Bad - write with no return and not last statement
func handler3(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
someOtherOperation()
}