Back to questions
JuniorTheoryCommon
What does the program print?
package main
import (
"fmt"
"strconv"
)
func main() {
n, err := strconv.Atoi("12a")
fmt.Println(n, err != nil)
}0 true
- Expecting
12:strconv.Atoidoes not stop at the first non-digit; it rejects the whole string and returns0with a non-nil error. - Expecting
false, as with C'satoi: Go reports a bad number through the second result, soerris notnilanderr != nilprintstrue.
- How would you print the reason
strconv.Atoirejected the string? - Why should the code not use
nwhenerris notnil?