Go语言规范(常量)

原文:http://golang.org/doc/go_spec.html
    翻译:红猎人 (zengsai@gmail.com)

Constants 常量[Top]

There are boolean constantsinteger constantsfloating-point constantscomplex constants, and string constants. Integer, floating-point, and complex constants are collectively called numeric constants.

有 布尔常量整型常量、 浮点常量复数常量 和 字符串常量。整型常量、浮点常量和复数常量统称 数字常量

A constant value is represented by an integerfloating-pointimaginarycharacter, or string literal, an identifier denoting a constant, a constant expression, or the result value of some built-in functions such as unsafe.Sizeof and cap or len applied to an array, len applied to a string constant, real and imag applied to a complex constant and cmplx applied to numeric constants. The boolean truth values are represented by the predeclared constants true and false. The predeclared identifieriota denotes an integer constant.

常量的值用 整型、 浮点型、 虚数型、 字符型 或 字符串型 字面值表示, 一个标识符就代表一个常量, 一个 量常表达式, 或内建函数的返回值比如应用于数组的unsafe.Sizeof 和 cap 或 len, 应用于字符串常量的 len, 应用于复数常量的 real 和 imag 和应用于数字的 cmplx。 布尔真值用两个预定义的常量 true 和 false 来表示。 预定义标识符 iota 代表一个整型常量。

In general, complex constants are a form of constant expression and are discussed in that section.

一般来说,复数常量由 常量表达式 组成,这会在相应章节有详细说明。

Numeric constants represent values of arbitrary precision and do not overflow.

数字常量可以表示任意精度并且不会溢出。

Constants may be typed or untyped. Literal constants, truefalseiota, and certain constant expressions containing only untyped constant operands are untyped.

常量分为 有类型 和无类型两种。 字面常量、truefalseiota 和特定的 常量表达式 仅包含操作符是无类型的无类型量常。

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. It is an error if the constant value cannot be accurately represented as a value of the respective type. For instance, 3.0 can be given any integer or any floating-point type, while 2147483648.0 (equal to 1<<31) can be given the types float32float64, or uint32 but not int32 or string.

常量可以通过 常量声明、 转换、 变量声明 时的隐式转换、 赋值、 或作为 表达式 中的操作数来显示的获得类型, 如果常量的值与其类型不相同的话,会出现错误。 如,3.0 可以看做是整型或浮点型,而 2147483648.0 (等于 1<<31) 可以被看做是 float32float64或 uint32 类型,但不可以看做 int32 或 string 类型。

There are no constants denoting the IEEE-754 infinity and not-a-number values, but the math package's InfNaNIsInf, and IsNaN functions return and test for those values at run time.

没有专门用来表示 IEEE-754 中定义的无穷和非数字值的常量, 但是 math 包 的 Inf、 NaN、 IsInf 和 IsNaN 方法会在运行时测试和返回这些值。

Implementation restriction: A compiler may implement numeric constants by choosing an internal representation with at least twice as many bits as any machine type; for floating-point values, both the mantissa and exponent must be twice as large.

实现限制:编译器可以选择至少两倍于机器类型大小的位数来代表数字常量; 如浮点值,尾数和指数都必须是其两倍。

 

转载于:https://my.oschina.net/zengsai/blog/4147

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值