英文:
Why is there no type mismatch error?
问题
我将用户输入的数字定义为 var input float64
,然后我输入一个整数,我期望会得到一个错误,但是我得到的是 err = <nil>
。我错过了什么?
这是输出结果:
C:\Go\src\play\exercise>go run exercise2.go
输入一个数字以计算其平方根:1
err = <nil>
n is 1:
英文:
I define the number to be input by user as var input float64
and I input an integer and I would 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 output:
C:\Go\src\play\exercise>go run exercise2.go
Enter a number to take its square root: 1
err = <nil>
n is 1:
答案1
得分: 3
在Go中,您可以从整数类型转换为浮点数类型。因此,没有理由不宽容。
在计算机中,鲁棒性原则是软件的一般设计准则:
在你所做的事情上保守,在你从别人那里接受的事情上宽容(通常被重新表述为“在你发送的事情上保守,在你接受的事情上宽容”)。
该原则也被称为Postel定律,以互联网先驱Jon Postel的名字命名,他在传输控制协议的早期规范中写道:
TCP实现应遵循一个鲁棒性的一般原则:在你所做的事情上保守,在你从别人那里接受的事情上宽容。
换句话说,发送命令或数据给其他机器(或同一台机器上的其他程序)的代码应完全符合规范,但接收输入的代码应接受非符合规范的输入,只要意义清楚。
英文:
> The Go Programming Language Specification
>
> Conversions
>
> Specific rules apply to (non-constant) conversions between numeric
> types.
>
> Conversions between numeric types
>
> For the conversion of non-constant numeric values, the following rules
> apply:
>
> 1. When converting between integer types, if the value is a signed
> integer, it is sign extended to implicit infinite precision;
> otherwise it is zero extended. It is then truncated to fit in the
> result type's size. For example, if v := uint16(0x10F0), then
> uint32(int8(v)) == 0xFFFFFFF0. The conversion always yields a valid
> value; there is no indication of overflow.
> 2. When converting a floating-point number to an integer, the fraction
> is discarded (truncation towards zero).
> 3. When converting an integer or floating-point number to a
> floating-point type, or a complex number to another complex type,
> the result value is rounded to the precision specified by the
> destination type. For instance, the value of a variable x of type
> float32 may be stored using additional precision beyond that of an
> IEEE-754 32-bit number, but float32(x) represents the result of
> rounding x's value to 32-bit precision. Similarly, x + 0.1 may use
> more than 32 bits of precision, but float32(x + 0.1) does not.
>
> In all non-constant conversions involving floating-point or complex
> values, if the result type cannot represent the value the conversion
> succeeds but the result value is implementation-dependent.
In Go, you can convert from an integer type to a floating-point type. Therefore, there is no reason not to be forgiving.
> Robustness principle
>
> In computing, the robustness principle is a general design guideline
> for software:
>
> Be conservative in what you do, be liberal in what you accept from others
> (often reworded as "Be conservative in what you send, be
> liberal in what you accept").
>
> The principle is also known as Postel's law, after Internet pioneer
> Jon Postel, who wrote in an early specification of the Transmission
> Control Protocol that:1
>
> TCP implementations should follow a general principle of robustness: be conservative in > what you do, be liberal in what you
> accept from others.
>
> In other words, code that sends commands or data to other machines (or
> to other programs on the same machine) should conform completely to
> the specifications, but code that receives input should accept
> non-conformant input as long as the meaning is clear.
答案2
得分: 1
Scanf扫描从标准输入读取的文本,根据格式将连续的以空格分隔的值存储到连续的参数中。它返回成功扫描的项目数。
英文:
> Scanf scans text read from standard input, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully scanned.
Scanf returns the number of things it read. In this case n == 1 because... you entered one token followed by a newline. Presumably, you want the value of input
, not n
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论