Which fix removes the bug?
var n int
var err error
if s != "" {
n, err := strconv.Atoi(s)
fmt.Println(n, err)
}
return n, err // always 0, nil
- Move
var err errorinto the if - Rename err to
ein the if block - Use
=instead of:=inside - Check
err != nilbefore return
:= declares new n and err that shadow the outer ones, so the outer variables are never assigned.
Play Problem Solving ▸