Why is there no type mismatch error?

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: 
+4
source share
2 answers

Go programming language specification

Conversions

Specific rules apply to (mutable) conversions between numeric types.

Conversions between numeric types

To convert non-constant numeric values, the following rules apply:

  • When converting between integer types, if the value is a signed integer, it is a character expanded to implicit infinite precision; otherwise it is zero. Then it is truncated to result in a type size. For example, if v: = uint16 (0x10F0), then uint32 (int8 (v)) == 0xFFFFFFF0. Conversion always gives true value; no signs of overflow.
  • When converting a floating-point number to an integer, it is discarded (truncated to zero).
  • When converting an integer or floating-point number to a floating-point type or a complex number for another complex type, the result value is rounded to the precision specified in the destination type. For example, the value of a variable x of type float32 can be stored using extra precision, except for the 32-bit number of IEEE-754, but float32 (x) represents the result of rounding x to 32-bit precision. Similarly, x + 0.1 can use more than 32 bits of precision, but float32 (x + 0.1) does not.

In all non-constant conversions using floating point or complex value, if the result type cannot represent the conversion value successfully, but the value of the result depends on the implementation.

In Go, you can convert from an integer type to a floating point type. Therefore, there is no reason not to forgive.

Reliability principle

In computing, the principle of reliability is a general design guide for software:

Be conservative in what you do, be liberal in what you accept from others (often worded as “Be conservative in what you send, liberal in what you accept”).

This principle is also known as the Postel law, after Internet pioneer John Postel, who wrote in the early specification of the transmission Protocol of Management, which: 1

TCP implementations must follow the general principle of reliability: be conservative in what you do, be liberal in what you accept from others.

In other words, the code that sends commands or data to other machines (or to other programs on the same machine) must fully comply with the specification, but the code that receives the input must accept inappropriate input, if the meaning is clear.

+3
source

Scanf scans text read from standard input, storing consecutive values, separated by spaces, in consecutive arguments determined by the format. It returns the number of successfully checked items.

Scanf returns the number of things read. In this case, n == 1, because ... you entered one token, and then a new line. Presumably you want the value of input , not n .

+1
source

Source: https://habr.com/ru/post/1489790/


All Articles