$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · hard · #235

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
  1. Move var err error into the if
  2. Rename err to e in the if block
  3. Use = instead of := inside
  4. Check err != nil before return
:= declares new n and err that shadow the outer ones, so the outer variables are never assigned.
Play Problem Solving ▸