1
0
mirror of https://github.com/golang/go synced 2024-11-22 05:54:40 -07:00

rewording around ideal and basic types

DELTA=355  (93 added, 85 deleted, 177 changed)
OCL=34904
CL=34998
This commit is contained in:
Robert Griesemer 2009-09-24 19:36:48 -07:00
parent ed6de5af4c
commit 19b1d35d4c

View File

@ -64,7 +64,8 @@ Open issues:
- no mechanism to declare a local type name: type T P.T - no mechanism to declare a local type name: type T P.T
Todo's: Todo
[ ] clarify: two equal lowercase identifiers from different packages denote different objects
[ ] need language about function/method calls and parameter passing rules [ ] need language about function/method calls and parameter passing rules
[ ] need to say something about "scope" of selectors? [ ] need to say something about "scope" of selectors?
[ ] clarify what a field name is in struct declarations [ ] clarify what a field name is in struct declarations
@ -78,7 +79,6 @@ Todo's:
though obvious though obvious
[ ] specify iteration direction for range clause [ ] specify iteration direction for range clause
[ ] review language on implicit dereferencing [ ] review language on implicit dereferencing
[ ] document T.m mechanism to obtain a function from a method
--> -->
@ -86,8 +86,7 @@ Todo's:
<p> <p>
This is a reference manual for the Go programming language. For This is a reference manual for the Go programming language. For
more information and other documents, see <a more information and other documents, see <a href="http://go/go">go/go</a>.
href="/">the Go home page</a>.
</p> </p>
<p> <p>
@ -258,9 +257,9 @@ The following character sequences represent <a href="#Operators">operators</a>,
<h3 id="Integer_literals">Integer literals</h3> <h3 id="Integer_literals">Integer literals</h3>
<p> <p>
An integer literal is a sequence of one or more digits in the An integer literal is a sequence of digits representing an
corresponding base, which may be 8, 10, or 16. An optional prefix <a href="#Constants">integer constant</a>.
sets a non-decimal base: <code>0</code> for octal, <code>0x</code> or An optional prefix sets a non-decimal base: <code>0</code> for octal, <code>0x</code> or
<code>0X</code> for hexadecimal. In hexadecimal literals, letters <code>0X</code> for hexadecimal. In hexadecimal literals, letters
<code>a-f</code> and <code>A-F</code> represent values 10 through 15. <code>a-f</code> and <code>A-F</code> represent values 10 through 15.
</p> </p>
@ -280,8 +279,9 @@ hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit } .
<h3 id="Floating-point_literals">Floating-point literals</h3> <h3 id="Floating-point_literals">Floating-point literals</h3>
<p> <p>
A floating-point literal is a decimal representation of a floating-point A floating-point literal is a decimal representation of a
number. It has an integer part, a decimal point, a fractional part, <a href="#Constants">floating-point constant</a>.
It has an integer part, a decimal point, a fractional part,
and an exponent part. The integer and fractional part comprise and an exponent part. The integer and fractional part comprise
decimal digits; the exponent part is an <code>e</code> or <code>E</code> decimal digits; the exponent part is an <code>e</code> or <code>E</code>
followed by an optionally signed decimal exponent. One of the followed by an optionally signed decimal exponent. One of the
@ -306,28 +306,12 @@ exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
.12345E+5 .12345E+5
</pre> </pre>
<h3 id="Ideal_numbers">Ideal numbers</h3>
<p>
Integer literals represent values of arbitrary precision, or <i>ideal
integers</i>. Similarly, floating-point literals represent values
of arbitrary precision, or <i>ideal floats</i>. These <i>ideal
numbers</i> have no size or named type and cannot overflow. However,
when (used in an expression) assigned to a variable or typed constant,
the destination must be able to represent the assigned value.
</p>
<p>
Implementation restriction: A compiler may implement ideal numbers
by choosing an internal representation with at least twice as many
bits as any machine type; for floats, both the mantissa and exponent
must be twice as large.
</p>
<h3 id="Character_literals">Character literals</h3> <h3 id="Character_literals">Character literals</h3>
<p> <p>
A character literal represents an integer value, typically a A character literal represents an <a href="#Constants">integer constant</a>,
Unicode code point, as one or more characters enclosed in single typically a Unicode code point, as one or more characters enclosed in single
quotes. Within the quotes, any character may appear except single quotes. Within the quotes, any character may appear except single
quote and newline. A single quoted character represents itself, quote and newline. A single quoted character represents itself,
while multi-character sequences beginning with a backslash encode while multi-character sequences beginning with a backslash encode
@ -389,6 +373,7 @@ big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit
hex_digit hex_digit hex_digit hex_digit . hex_digit hex_digit hex_digit hex_digit .
escaped_char = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `"` ) . escaped_char = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `"` ) .
</pre> </pre>
<pre> <pre>
'a' 'a'
'ä' 'ä'
@ -403,19 +388,13 @@ escaped_char = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `
'\U00101234' '\U00101234'
</pre> </pre>
<p>
The value of a character literal is an ideal integer, just as with
integer literals.
</p>
<h3 id="String_literals">String literals</h3> <h3 id="String_literals">String literals</h3>
<p> <p>
String literals represent <i>ideal string</i> values. Ideal strings do not A string literal represents a <a href="#Constants">string constant</a>
have a named type but they are compatible with type <code>string</code> obtained from concatenating a sequence of characters. There are two forms:
<a href="#Type_identity_and_compatibility">Type identity and compatibility</a>). raw string literals and interpreted string literals.
There are two forms: raw string literals and interpreted string
literals.
</p> </p>
<p> <p>
Raw string literals are character sequences between back quotes Raw string literals are character sequences between back quotes
@ -486,14 +465,63 @@ point), and will appear as two code points if placed in a string
literal. literal.
</p> </p>
<h3 id="Boolean_literals">Boolean literals</h3>
<h2 id="Constants">Constants</h2>
<p>There are <i>boolean constants</i>, <i>integer constants</i>, <i>floating-point constants</i>,
and <i>string constants</i>. Integer and floating-point constants are
collectively called <i>numeric constants</i>.
</p>
<p> <p>
A boolean literal is one of the predeclared constants A constant value is represented by an
<code>true</code> or <code>false</code>. The value of a boolean <a href="#Integer_literals">integer</a>,
literal is an <i>ideal bool</i>. <a href="#Floating-point_literals">floating-point</a>,
<a href="#Character_literals">character</a>, or
<a href="#String_literals">string</a> literal,
an identifier denoting a constant,
a <a href="#Constant_expressions">constant expression</a>, or
the result value of some built-in functions such as <code>unsafe.Sizeof</code>
and <code>cap</code> or <code>len</code> applied to an array,
or <code>len</code> applied to a string constant.
The boolean truth values are represented by the predeclared constants
<code>true</code> and <code>false</code>. The predeclared identifier
<a href="#Iota">iota</a> denotes an integer constant.
</p> </p>
<p>
Numeric constants represent values of arbitrary precision that
have no size and cannot overflow.
</p>
<p>
Constants may be <a href="#Types">typed</a> or untyped.
Literal constants, <code>true</code>, <code>false</code>, <code>iota</code>,
and certain <a href="#Constant_expressions">constant expressions</a>
containing only untyped constant operands are untyped.
</p>
<p>
A constant may be given a type explicitly by a <a href="#Constant_declarations">constant declaration</a>
or <a href="#Conversions">conversion</a>, or implicitly when used in a
<a href="#Variable_declarations">variable declaration</a> or an
<a href="#Assignments">assignment</a> or as an
operand in an <a href="#Expressions">expression</a>.
It is an error if the constant value
cannot be accurately represented as a value of the respective type.
For instance, <code>3.0</code> can be given any integer type but also any
floating-point type, while <code>-1e12</code> can be given the types
<code>float32</code>, <code>float64</code>, or even <code>int64</code> but
not <code>uint64</code> or <code>string</code>.
</p>
<p>
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.
</p>
<h2 id="Types">Types</h2> <h2 id="Types">Types</h2>
<p> <p>
@ -511,16 +539,14 @@ TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType
</pre> </pre>
<p> <p>
<i>Basic types</i> such as <code>int</code> are predeclared (§<a href="#Predeclared_identifiers">Predeclared identifiers</a>). Named instances of the boolean, numeric, and string types are <a href="#Predeclared_identifiers">predeclared</a>.
Other types may be constructed from these, recursively, Array, struct, pointer, function, interface, slice, map, and channel types may be constructed using type literals.
including arrays, structs, pointers, functions, interfaces, slices, maps, and
channels.
</p> </p>
<p> <p>
A type may have a <i>method set</i> associated with it A type may have a <i>method set</i> associated with it
<a href="#Interface_types">Interface types</a>, §<a href="#Method_declarations">Method declarations</a>). <a href="#Interface_types">Interface types</a>, §<a href="#Method_declarations">Method declarations</a>).
The method set of an interface type (§<a href="#Interface_types">Interface types</a>) is its interface. The method set of an <a href="#Interface_types">interface type</a> is its interface.
The method set of any other named type <code>T</code> The method set of any other named type <code>T</code>
consists of all methods with receiver consists of all methods with receiver
type <code>T</code>. type <code>T</code>.
@ -532,23 +558,26 @@ Any other type has an empty method set.
<p> <p>
The <i>static type</i> (or just <i>type</i>) of a variable is the The <i>static type</i> (or just <i>type</i>) of a variable is the
type defined by its declaration. Variables of interface type type defined by its declaration. Variables of interface type
<a href="#Interface_types">Interface types</a>) also have a distinct <i>dynamic type</i>, which also have a distinct <i>dynamic type</i>, which
is the actual type of the value stored in the variable at run-time. is the actual type of the value stored in the variable at run-time.
The dynamic type may vary during execution but is always compatible The dynamic type may vary during execution but is always assignment compatible
with the static type of the interface variable. For non-interface to the static type of the interface variable. For non-interface
types, the dynamic type is always the static type. types, the dynamic type is always the static type.
</p> </p>
<h3 id="Basic_types">Basic types</h3>
<p> <h3 id="Boolean_types">Boolean types</h3>
Basic types include traditional numeric types, booleans, and strings. All are predeclared.
</p> A <i>boolean type</i> represents the set of Boolean truth values
denoted by the predeclared constants <code>true</code>
and <code>false</code>. The predeclared boolean type is <code>bool</code>.
<h3 id="Numeric_types">Numeric types</h3> <h3 id="Numeric_types">Numeric types</h3>
<p> <p>
The architecture-independent numeric types are: A <i>numeric type</i> represents sets of integer or floating-point values.
The predeclared architecture-independent numeric types are:
</p> </p>
<pre class="grammar"> <pre class="grammar">
@ -562,8 +591,8 @@ int16 the set of all signed 16-bit integers (-32768 to 32767)
int32 the set of all signed 32-bit integers (-2147483648 to 2147483647) int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807) int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
float32 the set of all valid IEEE-754 32-bit floating point numbers float32 the set of all IEEE-754 32-bit floating-point numbers
float64 the set of all valid IEEE-754 64-bit floating point numbers float64 the set of all IEEE-754 64-bit floating-point numbers
byte familiar alias for uint8 byte familiar alias for uint8
</pre> </pre>
@ -575,7 +604,7 @@ as the two's complement of its absolute value.
</p> </p>
<p> <p>
There is also a set of numeric types with implementation-specific sizes: There is also a set of predeclared numeric types with implementation-specific sizes:
</p> </p>
<pre class="grammar"> <pre class="grammar">
@ -595,19 +624,13 @@ are not the same type even though they may have the same size on a
particular architecture. particular architecture.
<h3 id="Booleans">Booleans</h3> <h3 id="String_types">String types</h3>
The type <code>bool</code> comprises the Boolean truth values
represented by the predeclared constants <code>true</code>
and <code>false</code>.
<h3 id="Strings">Strings</h3>
<p> <p>
The <code>string</code> type represents the set of string values. A <i>string type</i> represents the set of string values.
Strings behave like arrays of bytes but are immutable: once created, Strings behave like arrays of bytes but are immutable: once created,
it is impossible to change the contents of a string. it is impossible to change the contents of a string.
The predeclared string type is <code>string</code>.
<p> <p>
The elements of strings have type <code>byte</code> and may be The elements of strings have type <code>byte</code> and may be
@ -616,7 +639,7 @@ illegal to take the address of such an element; if
<code>s[i]</code> is the <i>i</i>th byte of a <code>s[i]</code> is the <i>i</i>th byte of a
string, <code>&amp;s[i]</code> is invalid. The length of string string, <code>&amp;s[i]</code> is invalid. The length of string
<code>s</code> can be discovered using the built-in function <code>s</code> can be discovered using the built-in function
<code>len(s)</code>. The length is a compile-time constant if <code>s</code> <code>len</code>. The length is a compile-time constant if <code>s</code>
is a string literal. is a string literal.
</p> </p>
@ -1002,7 +1025,7 @@ A map value may be <code>nil</code>.
</p> </p>
<pre class="ebnf"> <pre class="ebnf">
MapType = "map" "[" KeyType "]" ValueType . MapType = "map" "[" KeyType "]" ElementType .
KeyType = Type . KeyType = Type .
ValueType = Type . ValueType = Type .
</pre> </pre>
@ -1010,7 +1033,7 @@ ValueType = Type .
<p> <p>
The comparison operators <code>==</code> and <code>!=</code> The comparison operators <code>==</code> and <code>!=</code>
<a href="#Comparison_operators">Comparison operators</a>) must be fully defined for operands of the <a href="#Comparison_operators">Comparison operators</a>) must be fully defined for operands of the
key type; thus the key type must be a basic, pointer, interface, key type; thus the key type must be a boolean, numeric, string, pointer, function, interface,
map, or channel type. If the key type is an interface type, these map, or channel type. If the key type is an interface type, these
comparison operators must be defined for the dynamic key values; comparison operators must be defined for the dynamic key values;
failure will cause a run-time error. failure will cause a run-time error.
@ -1109,9 +1132,7 @@ received, <code>closed(c)</code> returns true.
<p> <p>
Two types may be <i>identical</i>, <i>compatible</i>, or <i>incompatible</i>. Two types may be <i>identical</i>, <i>compatible</i>, or <i>incompatible</i>.
Two identical types are always compatible, but two compatible types may not be identical. Two identical types are always compatible, but two compatible types may not be identical.
Go is <i>type safe</i>: a value of one type cannot be assigned to a variable of an </p>
incompatible type, and two values of incompatible types cannot be mixed in
binary operations.</p>
<h3 id="Type_identity_and_compatibility">Type identity and compatibility</h3> <h3 id="Type_identity_and_compatibility">Type identity and compatibility</h3>
@ -1212,34 +1233,46 @@ they have different field names.
<h3 id="Assignment_compatibility">Assignment compatibility</h3> <h3 id="Assignment_compatibility">Assignment compatibility</h3>
<p> <p>
Values of any type may always be assigned to variables A value <code>v</code> of static type <code>V</code> is <i>assignment compatible</i>
of compatible static type. Some types and values have conditions under which they may with a type <code>T</code> if one of the following conditions applies:
be assigned to otherwise incompatible types:
</p> </p>
<ul> <ul>
<li> <li>
A value can be assigned to an interface variable if the static <code>V</code> is compatible with <code>T</code>.
type of the value implements the interface.
</li> </li>
<li> <li>
The predeclared constant <code>nil</code> can be assigned to any <code>T</code> is an interface type and
pointer, function, slice, map, channel, or interface variable. <code>V</code> <a href="#Interface_types">implements</a> <code>T</code>.
<li>
A pointer <code>p</code> to an array can be assigned to a slice variable
<code>v</code> with compatible element type
if the type of <code>p</code> or <code>v</code> is unnamed.
The slice variable then refers to the original array; the data is not copied.
</li> </li>
<li> <li>
A bidirectional channel <code>c</code> can be assigned to a channel variable <code>V</code> is a pointer to an array and <code>T</code> is a slice type
<code>v</code> with compatible channel value type with compatible element type and at least one of <code>V</code> or <code>T</code> is unnamed.
if the type of <code>c</code> or <code>v</code> is unnamed. After assignment, the slice variable refers to the original array; the elements are not
copied.
</li> </li>
<li> <li>
A value can always be assigned to the <a href="#Blank_identifier">blank identifier</a>. <code>V</code> is a bidirectional channel and <code>T</code> is a channel type
with compatible element type and at least one of <code>V</code> or <code>T</code> is unnamed.
</li> </li>
</ul> </ul>
<p>
An untyped <a href="#Constants">constant</a> <code>v</code>
is assignment compatible with type <code>T</code> if <code>v</code>
can be represented accurately as a value of type <code>T</code>.
</p>
<p>
The predeclared identifier <code>nil</code> is assignment compatible with any
pointer, function, slice, map, channel, or interface type and
represents the <a href="#The_zero_value">zero value<a> for that type.
</p>
<p>
Any value may be assigned to the <a href="#Blank_identifier">blank identifier</a>.
</p>
<h3 id="Comparison_compatibility">Comparison compatibility</h3> <h3 id="Comparison_compatibility">Comparison compatibility</h3>
<p> <p>
@ -1416,7 +1449,10 @@ Architecture-specific convenience types:
float int uint uintptr float int uint uintptr
Constants: Constants:
true false iota nil true false iota
Zero value:
nil
Functions: Functions:
cap close closed len make new panic panicln print println cap close closed len make new panic panicln print println
@ -1448,7 +1484,7 @@ any other identifier but the declaration does not introduce a new binding.
</p> </p>
<h3 id="Const_declarations">Const declarations</h3> <h3 id="Constant_declarations">Constant declarations</h3>
<p> <p>
A constant declaration binds a list of identifiers (the names of A constant declaration binds a list of identifiers (the names of
@ -1469,23 +1505,25 @@ ExpressionList = Expression { "," Expression } .
</pre> </pre>
<p> <p>
If the type is present, all constants take the type specified, and
the expressions must be <a href="#Assignment_compatibility">assignment compatible</a> with that type.
If the type is omitted, the constants take the If the type is omitted, the constants take the
individual types of the corresponding expressions, which may be individual types of the corresponding expressions.
an <a href="#Ideal_numbers">ideal number</a>, <a href="#String_literals">ideal string</a>, If the expression values are untyped <a href="#Constants">constants</a>,
or <a href="#Boolean_literals">ideal bool</a>. the declared constants remain untyped and the constant identifiers
If the type is present, all constants take the type specified, and the types denote the constant values. For instance, if the expression is a
of all the expressions must be assignment-compatible floating-point literal, the constant identifier denotes a floating-point
with that type. constant, even if the literal's fractional part is zero.
</p> </p>
<pre> <pre>
const Pi float64 = 3.14159265358979323846 const Pi float64 = 3.14159265358979323846
const E = 2.718281828 const zero = 0.0 // untyped floating-point constant
const ( const (
size int64 = 1024; size int64 = 1024;
eof = -1; eof = -1; // untyped integer constant
) )
const a, b, c = 3, 4, "foo" // a = 3, b = 4, c = "foo" const a, b, c = 3, 4, "foo" // a = 3, b = 4, c = "foo", untyped integer and string constants
const u, v float = 0, 3 // u = 0.0, v = 3.0 const u, v float = 0, 3 // u = 0.0, v = 3.0
</pre> </pre>
@ -1519,9 +1557,9 @@ const (
<p> <p>
Within a constant declaration, the predeclared pseudo-constant Within a constant declaration, the predeclared pseudo-constant
<code>iota</code> represents successive integers. It is reset to 0 <code>iota</code> represents successive untyped integer <a href="#Constants">
whenever the reserved word <code>const</code> appears in the source constants</a>. It is reset to 0 whenever the reserved word <code>const</code>
and increments with each semicolon. It can be used to construct a appears in the source and increments with each semicolon. It can be used to construct a
set of related constants: set of related constants:
</p> </p>
@ -1539,9 +1577,9 @@ const (
) )
const ( const (
u = iota * 42; // u == 0 (ideal integer) u = iota * 42; // u == 0 (untyped integer constant)
v float = iota * 42; // v == 42.0 (float) v float = iota * 42; // v == 42.0 (float constant)
w = iota * 42; // w == 84 (ideal integer) w = iota * 42; // w == 84 (untyped integer constant)
) )
const x = iota; // x == 0 (iota has been reset) const x = iota; // x == 0 (iota has been reset)
@ -1640,17 +1678,18 @@ of the expression list.
</p> </p>
<p> <p>
If the type is absent and the corresponding expression is a constant If the type is absent and the corresponding expression evaluates to an
expression of ideal integer, float, string or bool type, the type of the untyped <a href="#Constants">constant</a>, the type of the declared variable
declared variable is <code>int</code>, <code>float</code>, is <code>bool</code>, <code>int</code>, <code>float</code>, or <code>string</code>
<code>string</code>, or <code>bool</code> respectively: respectively, depending on whether the value is a boolean, integer,
floating-point, or string constant:
</p> </p>
<pre> <pre>
var b = true // t has type bool
var i = 0 // i has type int var i = 0 // i has type int
var f = 3.1415 // f has type float var f = 3.0 // f has type float
var s = "OMDB" // s has type string var s = "OMDB" // s has type string
var t = true // t has type bool
</pre> </pre>
<h3 id="Short_variable_declarations">Short variable declarations</h3> <h3 id="Short_variable_declarations">Short variable declarations</h3>
@ -1792,8 +1831,7 @@ However, a function declared this way is not a method.
<p> <p>
An expression specifies the computation of a value by applying An expression specifies the computation of a value by applying
operators and functions to operands. An expression has a value operators and functions to operands.
and a type.
</p> </p>
<h3 id="Operands">Operands</h3> <h3 id="Operands">Operands</h3>
@ -1807,17 +1845,6 @@ BasicLit = int_lit | float_lit | char_lit | StringLit .
</pre> </pre>
<h3 id="Constants">Constants</h3>
<p>
A <i>constant</i> is a literal of a basic type
(including the predeclared constants <code>true</code>, <code>false</code>
and <code>nil</code>
and values denoted by <code>iota</code>)
or a constant expression (§<a href="#Constant_expressions">Constant expressions</a>).
Constants have values that are known at compile time.
</p>
<h3 id="Qualified_identifiers">Qualified identifiers</h3> <h3 id="Qualified_identifiers">Qualified identifiers</h3>
<p> <p>
@ -2220,7 +2247,7 @@ or for <code>a</code> of type <code>S</code> where <code>S</code> is a <a href="
<p> <p>
For <code>a</code> of type <code>T</code> For <code>a</code> of type <code>T</code>
where <code>T</code> is a <a href="#Strings">string type</a>: where <code>T</code> is a <a href="#String_types">string type</a>:
</p> </p>
<ul> <ul>
<li><code>x</code> must be an integer value and <code>0 &lt;= x &lt; len(a)</code> <li><code>x</code> must be an integer value and <code>0 &lt;= x &lt; len(a)</code>
@ -2307,15 +2334,18 @@ s[1] == 3
</pre> </pre>
<p> <p>
The slice length must be non-negative. The slice length must not be negative.
For arrays or strings, the indexes For arrays or strings, the indexes
<code>lo</code> and <code>hi</code> must satisfy <code>lo</code> and <code>hi</code> must satisfy
0 &lt;= <code>lo</code> &lt;= <code>hi</code> &lt;= length; 0 &lt;= <code>lo</code> &lt;= <code>hi</code> &lt;= length;
for slices, the upper bound is the capacity rather than the length. for slices, the upper bound is the capacity rather than the length.
</p>
<p> <p>
If the sliced operand is a string, the result of the slice operation is another, new If the sliced operand is a string or slice, the result of the slice operation
<a href="#Strings">string</a>. If the sliced operand is an array or slice, the result is a string or slice of the same type.
of the slice operation is a <a href="#Slice_types">slice</a>. If the sliced operand is an array, the result of the slice operation is a slice
with the same element type as the array.
</p> </p>
@ -2482,13 +2512,12 @@ unary_op = "+" | "-" | "!" | "^" | "*" | "&amp;" | "&lt;-" .
</pre> </pre>
<p> <p>
Comparisons are discussed elsewhere Comparisons are discussed <a href="#Comparison_operators">elsewhere</a>.
<a href="#Comparison_compatibility">Comparison compatibility</a>). For other binary operators, the operand types must be identical
For other binary operators, the
operand types must be identical
<a href="#Properties_of_types_and_values">Properties of types and values</a>) <a href="#Properties_of_types_and_values">Properties of types and values</a>)
unless the operation involves unless the operation involves channels, shifts, or untyped <a href="#Constants">constants</a>.
channels, shifts, or ideal constants. For operations involving constants only, see the section on
<a href="#Constant_expressions">constant expressions</a>.
</p> </p>
<p> <p>
@ -2498,24 +2527,20 @@ second is a value of the channel's element type.
<p> <p>
Except for shift operations, Except for shift operations,
if one operand has ideal type and the other operand does not, if one operand is an untyped <a href="#Constants">constant</a>
the ideal operand is converted to match the type of and the other operand is not, the constant is <a href="#Conversions">converted</a>
the other operand (§<a href="#Expressions">Expressions</a>). to the type of the other operand.
If both operands are ideal numbers and one is an
ideal float, the other is converted to ideal float
(relevant for <code>/</code> and <code>%</code>).
</p> </p>
<p> <p>
The right operand in a shift operation must have unsigned integer type The right operand in a shift operation must have unsigned integer type
or be an ideal number that can be converted to unsigned integer type or be an untyped constant that can be converted to unsigned integer type.
<a href="#Arithmetic_operators">Arithmetic operators</a>).
</p> </p>
<p> <p>
If the left operand of a non-constant shift operation is an ideal number, If the left operand of a non-constant shift operation is an untyped constant,
the type of the ideal number the type of constant is what it would be if the shift operation were replaced by
is what it would be if the shift operation were replaced by the left operand alone. the left operand alone.
</p> </p>
<pre> <pre>
@ -2568,11 +2593,11 @@ x == y+1 &amp;&amp; &lt;-chan_ptr > 0
<h3 id="Arithmetic_operators">Arithmetic operators</h3> <h3 id="Arithmetic_operators">Arithmetic operators</h3>
<p> <p>
Arithmetic operators apply to numeric types and yield a result of the same Arithmetic operators apply to numeric values and yield a result of the same
type as the first operand. The four standard arithmetic operators (<code>+</code>, type as the first operand. The four standard arithmetic operators (<code>+</code>,
<code>-</code>, <code>*</code>, <code>/</code>) apply both to integer and <code>-</code>, <code>*</code>, <code>/</code>) apply to integer and
floating point types, while <code>+</code> applies also floating-point types; <code>+</code> also applies
to strings; all other arithmetic operators apply to integers only. to strings. All other arithmetic operators apply to integers only.
</p> </p>
<pre class="grammar"> <pre class="grammar">
@ -2663,7 +2688,7 @@ follows:
</pre> </pre>
<p> <p>
For floating point numbers, For floating-point numbers,
<code>+x</code> is the same as <code>x</code>, <code>+x</code> is the same as <code>x</code>,
while <code>-x</code> is the negation of <code>x</code>. while <code>-x</code> is the negation of <code>x</code>.
</p> </p>
@ -2692,11 +2717,10 @@ not occur. For instance, it may not assume that <code>x &lt; x + 1</code> is alw
<h3 id="Comparison_operators">Comparison operators</h3> <h3 id="Comparison_operators">Comparison operators</h3>
<p> <p>
Comparison operators yield a boolean result. Comparison operators yield a value of type <code>bool</code>.
The operators <code>==</code> and <code>!=</code> apply, at least in some cases, The operators <code>==</code> and <code>!=</code> apply, at least in some cases,
to all types except arrays and structs. to operands of all types except arrays and structs.
All other comparison operators apply only All other comparison operators apply only to numeric and string values.
to basic types except <code>bool</code>.
</p> </p>
<pre class="grammar"> <pre class="grammar">
@ -2709,13 +2733,14 @@ to basic types except <code>bool</code>.
</pre> </pre>
<p> <p>
Numeric basic types are compared in the usual way. Operands of numeric type are compared in the usual way.
</p> </p>
<p> <p>
Strings are compared byte-wise (lexically). Operands of string type are compared byte-wise (lexically).
</p> </p>
<p> <p>
Booleans are equal if they are either both "true" or both "false". Operands of boolean type are equal if they are either both <code>true</code>
or both <code>false</code>.
</p> </p>
<p> <p>
The rules for comparison of composite types are described in the The rules for comparison of composite types are described in the
@ -2726,7 +2751,8 @@ section on §<a href="#Comparison_compatibility">Comparison compatibility</a>.
<h3 id="Logical_operators">Logical operators</h3> <h3 id="Logical_operators">Logical operators</h3>
<p> <p>
Logical operators apply to boolean operands and yield a boolean result. Logical operators apply to <a href="#Boolean_types">boolean</a> values
and yield a result of the same type as the operands.
The right operand is evaluated conditionally. The right operand is evaluated conditionally.
</p> </p>
@ -2972,36 +2998,31 @@ The resulting function takes an explicit receiver of that interface type.
<h3 id="Constant_expressions">Constant expressions</h3> <h3 id="Constant_expressions">Constant expressions</h3>
<p> <p>
Constant expressions may contain only constants, <code>iota</code>, Constant expressions may contain only <a href="#Constants">constant</a>
numeric literals, string literals, and operands and are evaluated at compile-time.
some constant-valued built-in functions such as <code>unsafe.Sizeof</code>
and <code>len</code> applied to an array.
In practice, constant expressions are those that can be evaluated at compile time.
<p>
The type of a constant expression is determined by the type of its
elements. If it contains only numeric literals, its type is <i>ideal
integer</i> or <i>ideal float</i><a href="#Ideal_numbers">Ideal numbers</a>). Whether a literal
is an integer or float depends on the syntax of the literals (123 vs. 123.0).
The nature of the arithmetic
operations within the expression depends, elementwise, on the values;
for example, 3/2 is an integer division yielding 1, while 3./2. is
a floating point division yielding 1.5. Thus
</p>
<pre>
const x = 3./2. + 3/2;
</pre>
<p>
yields a floating point constant of ideal float value 2.5 (1.5 +
1); its constituent expressions are evaluated using distinct rules
for division.
</p> </p>
<p> <p>
Intermediate values and the constants themselves Untyped boolean, numeric, and string constants may be used as operands
may require precision significantly larger than any concrete type wherever it is legal to use an operand of boolean, numeric, or string type,
in the language. The following are legal declarations: respectively. Except for shift operations, if the operands of a binary operation
are an untyped integer constant and an untyped floating-point constant,
the integer constant is converted to an untyped floating-point constant
(relevant for <code>/</code> and <code>%</code>).
<p>
</p>
Applying an operator to untyped constants results in an untyped
constant of the same kind (that is, a boolean, integer, floating-point, or
string constant), except for
<a href="#Comparison_operators">comparison operators</a> which result in
a constant of type <code>bool</code>.
</p>
<p>
Constant expressions are always evaluated exactly; intermediate values and the
constants themselves may require precision significantly larger than supported
by any predeclared type in the language. The following are legal declarations:
</p> </p>
<pre> <pre>
@ -3010,38 +3031,26 @@ const Four int8 = Huge &gt;&gt; 98;
</pre> </pre>
<p> <p>
A constant expression may appear in any context, such as assignment The values of <i>typed</i> constants must always be accurately representable as values
to a variable of any numeric type, as long as the value of the of the constant type. The following constant expressions are illegal:
expression can be represented accurately in that context.
It is erroneous to assign a value with a non-zero fractional part
to an integer, or if the assignment would overflow or underflow,
or in general if the value cannot be represented by the type of
the variable.
For
instance, <code>3</code> can be assigned to any integer variable but also to any
floating point variable, while <code>-1e12</code> can be assigned to a
<code>float32</code>, <code>float64</code>, or even <code>int64</code>
but not <code>uint64</code> or <code>string</code>.
</p>
<p>
If a typed constant expression evaluates to a value that is not
representable by that type, the compiler reports an error.
</p> </p>
<pre> <pre>
uint8(-1) // error, out of range uint(-1) // -1 overflows uint
uint8(100) * 100 // error, out of range int(3.14) // 3.14 truncated to integer
int64(Huge) // 1&lt;&lt;100 overflows int64
Four * 300 // 300 overflows int8
Four * 100 // 400 overflows int8
</pre> </pre>
<p> <p>
The mask used by the unary bitwise complement operator matches The mask used by the unary bitwise complement operator <code>^</code> matches
the rule for non-constants: the mask is all 1s for unsigned constants the rule for non-constants: the mask is all 1s for unsigned constants
and -1 for signed and ideal constants. and -1 for signed and untyped constants.
</p> </p>
<pre> <pre>
^1 // ideal constant, equal to -2 ^1 // untyped integer constant, equal to -2
uint8(^1) // error, same as uint8(-2), out of range uint8(^1) // error, same as uint8(-2), out of range
^uint8(1) // typed uint8 constant, same as 0xFF ^ uint8(1) = uint8(0xFE) ^uint8(1) // typed uint8 constant, same as 0xFF ^ uint8(1) = uint8(0xFE)
int8(^1) // same as int8(-2) int8(^1) // same as int8(-2)
@ -3056,6 +3065,7 @@ overflow etc. errors being caught.
</font> </font>
</p> </p>
<h3 id="Order_of_evaluation">Order of evaluation</h3> <h3 id="Order_of_evaluation">Order of evaluation</h3>
<p> <p>
@ -3164,8 +3174,9 @@ f(x+y)
<p> <p>
The "++" and "--" statements increment or decrement their operands The "++" and "--" statements increment or decrement their operands
by the ideal numeric value 1. As with an assignment, the operand by the untyped <a href="#Constants">constant</a> <code>1</code>.
must be a variable, pointer indirection, field selector or index expression. As with an assignment, the operand must be a variable, pointer indirection,
field selector or index expression.
</p> </p>
<pre class="ebnf"> <pre class="ebnf">
@ -3259,9 +3270,13 @@ a, b = b, a // exchange a and b
</pre> </pre>
<p> <p>
In assignments, the type of each value must be In assignments, each value must be
<a href="#Assignment_compatibility">assignment compatible</a> with the type of the <a href="#Assignment_compatibility">assignment compatible</a> with the type of the
operand to which it is assigned. operand to which it is assigned. If an untyped <a href="#Constants">constant</a>
is assigned to a variable of interface type, the constant is <a href="#Conversions">converted</a>
to type <code>bool</code>, <code>int</code>, <code>float</code>, or <code>string</code>
respectively, depending on whether the value is a boolean, integer, floating-point,
or string constant.
</p> </p>
@ -3599,7 +3614,7 @@ for i, s := range a {
} }
var key string; var key string;
var val interface {}; // value type of m is assignment-compatible to val var val interface {}; // value type of m is assignment compatible to val
for key, val = range m { for key, val = range m {
h(key, val) h(key, val)
} }
@ -3967,32 +3982,26 @@ The following conversion rules apply:
</p> </p>
<ul> <ul>
<li> <li>
1) The conversion succeeds if the value is assignment-compatible 1) The conversion succeeds if the value is <a href="#Assignment_compatibility">assignment compatible</a>
to a variable of type T. with type <code>T</code>.
</li> </li>
<li> <li>
2) The conversion succeeds if the value would be assignment-compatible 2) The conversion succeeds if the value would be assignment compatible
to a variable of type T if the value's type, or T, or any of their component with type <code>T</code> if the value's type, or <code>T</code>, or any of their component
types are unnamed (§<a href="#Type_identity_and_compatibility">Type identity and compatibility</a>). types were unnamed (§<a href="#Type_identity_and_compatibility">Type identity and compatibility</a>).
</li> </li>
<li> <li>
3a) From an ideal number to an integer type. 3) Between integer types. If the value is a signed quantity, it is
The ideal number must be representable in the result type; it must not overflow.
For example, <code>uint8(0xFF)</code> is legal but <code>int8(0xFF)</code> is not.
</li>
<li>
3b) From a non-ideal integer value to an integer type. If the value is a signed quantity, it is
sign extended to implicit infinite precision; otherwise it is zero sign extended to implicit infinite precision; otherwise it is zero
extended. It is then truncated to fit in the result type's size. extended. It is then truncated to fit in the result type's size.
For example, if <code>x := uint16(0x10F0)</code>, then <code>uint32(int8(x)) == 0xFFFFFFF0</code>. For example, if <code>x := uint16(0x10F0)</code>, then <code>uint32(int8(x)) == 0xFFFFFFF0</code>.
The conversion always yields a valid value; there is no indication of overflow. The conversion always yields a valid value; there is no indication of overflow.
</li> </li>
<li> <li>
4) Between integer and floating point types, or between floating point 4) Between integer and floating-point types, or between floating-point types.
types. When converting a floating-point number to an integer, the fraction is discarded
When converting a floating point number to an integer, the fraction is discarded
(truncation towards zero). (truncation towards zero).
In all conversions involving floating point, if the result type cannot represent the In all conversions involving floating-point values, if the result type cannot represent the
value the conversion succeeds but the result value is unspecified. value the conversion succeeds but the result value is unspecified.
<font color=red>This behavior may change.</font> <font color=red>This behavior may change.</font>
</li> </li>
@ -4029,9 +4038,9 @@ string([]byte{'h', 'e', 'l', 'l', 'o'}) // "hello"
<p> <p>
There is no linguistic mechanism to convert between pointers and integers. There is no linguistic mechanism to convert between pointers and integers.
The <code>unsafe</code> package The package <a href="#Package_unsafe"><code>unsafe</code></a>
implements this functionality under implements this functionality under
restricted circumstances<a href="#Package_unsafe">Package <code>unsafe</code></a>). restricted circumstances.
</p> </p>
@ -4287,7 +4296,7 @@ and no explicit initialization is provided, the memory is
given a default initialization. Each element of such a value is given a default initialization. Each element of such a value is
set to the <i>zero value</i> for its type: <code>false</code> for booleans, set to the <i>zero value</i> for its type: <code>false</code> for booleans,
<code>0</code> for integers, <code>0.0</code> for floats, <code>""</code> <code>0</code> for integers, <code>0.0</code> for floats, <code>""</code>
for strings, and <code>nil</code> for pointers, interfaces, slices, channels, and maps. for strings, and <code>nil</code> for pointers, functions, interfaces, slices, channels, and maps.
This initialization is done recursively, so for instance each element of an This initialization is done recursively, so for instance each element of an
array of structs will have its fields zeroed if no value is specified. array of structs will have its fields zeroed if no value is specified.
</p> </p>