在Go语言中创建新类型的困惑

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

Creation new types in go confusion

问题

我有这样的代码:

type ErrNegativeSqrt float64

为什么会有这样的构造?

float64(ErrNegativeSqrt(-2))

ErrNegativeSqrt中,使用了什么机制来存储-2?

英文:

I have such code:

type ErrNegativeSqrt float64

Why such construct is available?

float64(ErrNegativeSqrt(-2))

Which 'mechanics' is used to store -2 in ErrNegativeSqrt?

答案1

得分: 4

ErrNegativeSqrt是一个类型而不是一个变量。值存储在变量中。

type ErrNegativeSqrt float64
// x是类型为ErrNegativeSqrt的变量,初始值为-2
var x ErrNegativeSqrt = -2

更新:

> Go编程语言规范
>
> 常量
>
> 有布尔常量、符文常量、整数常量、浮点数常量、复数常量和字符串常量。
> 字符、整数、浮点数和复数常量统称为数值常量。
>
> 常量值由符文、整数、浮点数、虚数或字符串字面量、表示常量的标识符、常量表达式、结果为常量的转换或应用于任何值的一些内置函数(如unsafe.Sizeof)的结果、应用于某些表达式的cap或len、应用于复数常量的real和imag以及应用于数值常量的complex来表示。布尔真值由预声明的常量true和false表示。预声明的标识符iota表示一个整数常量。
>
> 数值常量表示任意精度的值,不会溢出。
>
> 常量可以是有类型的或无类型的。字面常量、true、false、iota以及只包含无类型常量操作数的某些常量表达式是无类型的。
>
> 常量可以通过常量声明或转换显式地给定类型,也可以在变量声明、赋值或表达式中隐式地给定类型。
>
> 转换
>
> 转换是形式为T(x)的表达式,其中T是类型,x是可以转换为类型T的表达式。

ErrNegativeSqrt(-2)是一个转换。无类型常量-2被转换为类型ErrNegativeSqrt,因为作为操作数,它可以表示在ErrNegativeSqrtfloat64基础类型中。

英文:

ErrNegativeSqrt is a type not a variable. Values are stored in variables.

type ErrNegativeSqrt float64
// x is a variable of type ErrNegativeSqrt with an initial value of -2
var x ErrNegativeSqrt = -2

UPDATE:

> The Go Programming Language Specification
>
> Constants
>
> There are boolean constants, rune constants, integer constants,
> floating-point constants, complex constants, and string constants.
> Character, integer, floating-point, and complex constants are
> collectively called numeric constants.
>
> A constant value is represented by a rune, integer, floating-point,
> imaginary, or string literal, an identifier denoting a constant, a
> constant expression, a conversion with a result that is a constant, or
> the result value of some built-in functions such as unsafe.Sizeof
> applied to any value, cap or len applied to some expressions, real and
> imag applied to a complex constant and complex applied to numeric
> constants. The boolean truth values are represented by the predeclared
> constants true and false. The predeclared identifier iota denotes an
> integer constant.
>
> Numeric constants represent values of arbitrary precision and do not
> overflow.
>
> Constants may be typed or untyped. Literal constants, true, false,
> iota, and certain constant expressions containing only untyped
> constant operands are untyped.
>
> A constant may be given a type explicitly by a constant declaration or
> conversion, or implicitly when used in a variable declaration or an
> assignment or as an operand in an expression.
>
> Conversions
>
> Conversions are expressions of the form T(x) where T is a type and x
> is an expression that can be converted to type T.

ErrNegativeSqrt(-2) is a conversion. The untyped constant -2 is converted to type ErrNegativeSqrt because, as an operand, it can be represented in ErrNegativeSqrt's float64 underlying type.

答案2

得分: 1

ErrNegativeSqrt(-2)是可能的,因为ErrNegativeSqrt在内部是一个float64,根据规范描述,它能够保存"IEEE-754 64位浮点数"。

http://en.wikipedia.org/wiki/IEEE_floating_point

英文:

ErrNegativeSqrt(-2) is possible because ErrNegativeSqrt is a float64 internally, which is described by the spec as able to hold "IEEE-754 64-bit floating-point numbers".

http://en.wikipedia.org/wiki/IEEE_floating_point

答案3

得分: 1

这被称为转换,参见这里:http://golang.org/ref/spec#Conversions。它描述了当一个值与另一个值兼容时,如何通过将其转换为该类型来创建一个值。在这里,-2可以表示为float64类型。

英文:

This is called conversions, see here: http://golang.org/ref/spec#Conversions . It describes how a value can be created by converting another value into that type when it i compatible. And here the -2 can be represented as float64.

huangapple
  • 本文由 发表于 2013年7月4日 01:45:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/17454837.html
匿名

发表评论

匿名网友

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

确定