I am writing a handler to render a GIF of a Lorentz attractor. I need to parse floating point numbers from the some querystrings attached to the GIF path. If any of them are bad, I need to log an error.
func lorenzHandler(w http.ResponseWriter, r *http.Request) {
var e error
rho := atof(r.FormValue("r"), &e)
sigma := atof(r.FormValue("s"), &e)
b := atof(r.FormValue("b"), &e)
if e != nil {
log.Printf("bad parameters: r=%s; s=%s; b=%s;\n", r.FormValue("r"), r.FormValue("s"), r.FormValue("b"))
} else {
// draw lorenz GIF with parameters rho, sigma and b
// but until then...
log.Println("yay!", rho, sigma, b)
}
}
func atof(s string, e *error) float32 {
if *e == nil {
var x float64
x, *e = strconv.ParseFloat(s, 32)
return float32(x)
}
return 0
}
Is passing around an error, then cleaning it up, correct?