diff options
Diffstat (limited to 'doc/go_spec.html')
-rw-r--r-- | doc/go_spec.html | 137 |
1 files changed, 101 insertions, 36 deletions
diff --git a/doc/go_spec.html b/doc/go_spec.html index abf5b8f50..489ad4db3 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,5 +1,5 @@ <!-- title The Go Programming Language Specification --> -<!-- subtitle Version of June 7, 2011 --> +<!-- subtitle Version of June 17, 2011 --> <!-- TODO @@ -10,7 +10,6 @@ TODO [ ] clarify what a field name is in struct declarations (struct{T} vs struct {T T} vs struct {t T}) [ ] need explicit language about the result type of operations -[ ] should string(1<<s) and float32(1<<s) be valid? [ ] should probably write something about evaluation order of statements even though obvious [ ] review language on implicit dereferencing @@ -529,7 +528,8 @@ A constant value is represented by an <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 +a <a href="#Constant_expressions">constant expression</a>, +a <a href="#Conversions">conversion</a> with a result that is a constant, or the result value of some built-in functions such as <code>unsafe.Sizeof</code> applied to any value, <code>cap</code> or <code>len</code> applied to @@ -1452,6 +1452,7 @@ Go is lexically scoped using blocks: <li>The scope of a constant or variable identifier declared inside a function begins at the end of the ConstSpec or VarSpec + (ShortVarDecl for short variable declarations) and ends at the end of the innermost containing block.</li> <li>The scope of a type identifier declared inside a function @@ -3227,8 +3228,42 @@ If the type starts with an operator it must be parenthesized: </pre> <p> -A value <code>x</code> can be converted to type <code>T</code> in any -of these cases: +A <a href="#Constants">constant</a> value <code>x</code> can be converted to +type <code>T</code> in any of these cases: +</p> + +<ul> + <li> + <code>x</code> is representable by a value of type <code>T</code>. + </li> + <li> + <code>x</code> is an integer constant and <code>T</code> is a + <a href="#String_types">string type</a>. + The same rule as for non-constant <code>x</code> applies in this case + (§<a href="#Conversions_to_and_from_a_string_type">Conversions to and from a string type</a>). + </li> +</ul> + +<p> +Converting a constant yields a typed constant as result. +</p> + +<pre> +uint(iota) // iota value of type uint +float32(2.718281828) // 2.718281828 of type float32 +complex128(1) // 1.0 + 0.0i of type complex128 +string('x') // "x" of type string +string(0x266c) // "♬" of type string +MyString("foo" + "bar") // "foobar" of type MyString +string([]byte{'a'}) // not a constant: []byte{'a'} is not a constant +(*int)(nil) // not a constant: nil is not a constant, *int is not a boolean, numeric, or string type +int(1.2) // illegal: 1.2 cannot be represented as an int +string(65.0) // illegal: 65.0 is not an integer constant +</pre> + +<p> +A non-constant value <code>x</code> can be converted to type <code>T</code> +in any of these cases: </p> <ul> @@ -3262,15 +3297,27 @@ of these cases: </ul> <p> -Specific rules apply to conversions between numeric types or to and from -a string type. +Specific rules apply to (non-constant) conversions between numeric types or +to and from a string type. These conversions may change the representation of <code>x</code> and incur a run-time cost. All other conversions only change the type but not the representation of <code>x</code>. </p> +<p> +There is no linguistic mechanism to convert between pointers and integers. +The package <a href="#Package_unsafe"><code>unsafe</code></a> +implements this functionality under +restricted circumstances. +</p> + <h4>Conversions between numeric types</h4> + +<p> +For the conversion of non-constant numeric values, the following rules apply: +</p> + <ol> <li> When converting between integer types, if the value is a signed integer, it is @@ -3296,13 +3343,12 @@ of precision, but <code>float32(x + 0.1)</code> does not. </ol> <p> -In all conversions involving floating-point or complex values, +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. +succeeds but the result value is implementation-dependent. </p> -<h4>Conversions to and from a string type</h4> +<h4 id="Conversions_to_and_from_a_string_type">Conversions to and from a string type</h4> <ol> <li> @@ -3360,12 +3406,6 @@ If the string is empty, the result is <code>[]int(nil)</code>. </li> </ol> -<p> -There is no linguistic mechanism to convert between pointers and integers. -The package <a href="#Package_unsafe"><code>unsafe</code></a> -implements this functionality under -restricted circumstances. -</p> <h3 id="Constant_expressions">Constant expressions</h3> @@ -4353,8 +4393,8 @@ goto Error <p> Executing the "goto" statement must not cause any variables to come into -scope that were not already in scope at the point of the goto. For -instance, this example: +<a href="#Declarations_and_scope">scope</a> that were not already in scope at the point of the goto. +For instance, this example: </p> <pre> @@ -4366,9 +4406,29 @@ L: <p> is erroneous because the jump to label <code>L</code> skips the creation of <code>v</code>. -<!-- -(<span class="alert">TODO: Eliminate in favor of used and not set errors?</span>) ---> +</p> + +<p> +A "goto" statement outside a <a href="#Blocks">block</a> cannot jump to a label inside that block. +For instance, this example: +</p> + +<pre> +if n%2 == 1 { + goto L1 +} +for n > 0 { + f() + n-- +L1: + f() + n-- +} +</pre> + +<p> +is erroneous because the label <code>L1</code> is inside +the "for" statement's block but the <code>goto</code> is not. </p> <h3 id="Fallthrough_statements">Fallthrough statements</h3> @@ -4590,11 +4650,14 @@ Two built-in functions assist in common slice operations. </p> <p> -The function <code>append</code> appends zero or more values <code>x</code> +The <a href="#Function_types">variadic</a> function <code>append</code> +appends zero or more values <code>x</code> to <code>s</code> of type <code>S</code>, which must be a slice type, and returns the resulting slice, also of type <code>S</code>. -Each value <code>x</code> must be <a href="#Assignability">assignable</a> to -the <a href="#Slice_types">element type</a> of <code>S</code>. +The values <code>x</code> are passed to a parameter of type <code>...T</code> +where <code>T</code> is the <a href="#Slice_types">element type</a> of +<code>S</code> and the respective +<a href="#Passing_arguments_to_..._parameters">parameter passing rules</a> apply. </p> <pre class="grammar"> @@ -5102,9 +5165,9 @@ package unsafe type ArbitraryType int // shorthand for an arbitrary Go type; it is not a real type type Pointer *ArbitraryType -func Alignof(variable ArbitraryType) int -func Offsetof(selector ArbitraryType) int -func Sizeof(variable ArbitraryType) int +func Alignof(variable ArbitraryType) uintptr +func Offsetof(selector ArbitraryType) uinptr +func Sizeof(variable ArbitraryType) uintptr func Reflect(val interface{}) (typ runtime.Type, addr uintptr) func Typeof(val interface{}) (typ interface{}) @@ -5127,7 +5190,7 @@ For a struct <code>s</code> with field <code>f</code>: </p> <pre> -uintptr(unsafe.Pointer(&s)) + uintptr(unsafe.Offsetof(s.f)) == uintptr(unsafe.Pointer(&s.f)) +uintptr(unsafe.Pointer(&s)) + unsafe.Offsetof(s.f) == uintptr(unsafe.Pointer(&s.f)) </pre> <p> @@ -5140,12 +5203,12 @@ alignment of the (type of the) variable in bytes. For a variable </p> <pre> -uintptr(unsafe.Pointer(&x)) % uintptr(unsafe.Alignof(x)) == 0 +uintptr(unsafe.Pointer(&x)) % unsafe.Alignof(x) == 0 </pre> <p> Calls to <code>Alignof</code>, <code>Offsetof</code>, and -<code>Sizeof</code> are compile-time constant expressions of type <code>int</code>. +<code>Sizeof</code> are compile-time constant expressions of type <code>uintptr</code>. </p> <p> The functions <code>unsafe.Typeof</code>, @@ -5198,10 +5261,12 @@ The following minimal alignment properties are guaranteed: </li> </ol> -<h2 id="Implementation_differences"><span class="alert">Implementation differences - TODO</span></h2> +<span class="alert"> +<h2 id="Implementation_differences">Implementation differences - TODO</h2> <ul> - <li><span class="alert">The restriction on <code>goto</code> statements and targets (no intervening declarations) is not honored.</span></li> - <li><span class="alert"><code>len(a)</code> is only a constant if <code>a</code> is a (qualified) identifier denoting an array or pointer to an array.</span></li> - <li><span class="alert"><code>nil</code> maps are not treated like empty maps.</span></li> - <li><span class="alert">Trying to send/receive from a <code>nil</code> channel causes a run-time panic.</span></li> + <li><code>len(a)</code> is only a constant if <code>a</code> is a (qualified) identifier denoting an array or pointer to an array.</li> + <li><code>nil</code> maps are not treated like empty maps.</li> + <li>Trying to send/receive from a <code>nil</code> channel causes a run-time panic.</li> + <li><code>unsafe.Alignof</code>, <code>unsafe.Offsetof</code>, and <code>unsafe.Sizeof</code> return an <code>int</code>.</li> </ul> +</span> |