为什么没有类型不匹配的错误?

huangapple go评论69阅读模式
英文:

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 = &lt;nil&gt;. What am I missing?

package main

import (
	&quot;fmt&quot;
)

func main() {
	var input float64 

	fmt.Print(&quot;Enter a number:&quot;)

	n, err := fmt.Scanf(&quot;%f\n&quot;, &amp;input)

	fmt.Printf(&quot;err = %v\n&quot;, err)

	if err != nil {
		fmt.Printf(&quot;%v is not a float - exiting with error\n&quot;, input, err)
		return
	}

    fmt.Printf(&quot;n is %v:&quot;, n)
}

This is the output:

C:\Go\src\play\exercise&gt;go run exercise2.go
Enter a number to take its square root: 1
err = &lt;nil&gt;
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.

huangapple
  • 本文由 发表于 2013年7月5日 03:51:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/17477332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定