I define the number that the user will enter as var input float64 , and I enter an integer, and I expect to get an error, but I get err = <nil> . What am I missing?
package main import ( "fmt" ) func main() { var input float64 fmt.Print("Enter a number:") n, err := fmt.Scanf("%f\n", &input) fmt.Printf("err = %v\n", err) if err != nil { fmt.Printf("%v is not a float - exiting with error\n", input, err) return } fmt.Printf("n is %v:", n) }
This is the conclusion:
C:\Go\src\play\exercise>go run exercise2.go Enter a number to take its square root: 1 err = <nil> n is 1:
source share