From 9d7074acade701c97219c5aafb68a908ae76fb47 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 16 Oct 2014 15:08:49 -0700 Subject: spec: define "variable" Fixes issue 8496. LGTM=rsc, r, iant R=r, rsc, iant, ken CC=golang-codereviews https://codereview.appspot.com/148580043 --- doc/go_spec.html | 103 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 78 insertions(+), 25 deletions(-) (limited to 'doc') diff --git a/doc/go_spec.html b/doc/go_spec.html index 7fa02e419..97effeaa4 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -647,6 +647,65 @@ of evaluating constant expressions.

+

Variables

+ +

+A variable is a storage location for holding a value. +The set of permissible values is determined by the +variable's type. +

+ +

+A variable declaration +or, for function parameters and results, the signature +of a function declaration +or function literal reserves +storage for a named variable. + +Calling the built-in function new +or taking the address of a composite literal +allocates storage for a variable at run time. +Such an anonymous variable is referred to via a (possibly implicit) +pointer indirection. +

+ +

+Structured variables of array, slice, +and struct types have elements and fields that may +be addressed individually. Each such element +acts like a variable. +

+ +

+The static type (or just type) of a variable is the +type given in its declaration, the type provided in the +new call or composite literal, or the type of +an element of a structured variable. +Variables of interface type also have a distinct dynamic type, +which is the concrete type of the value assigned to the variable at run time +(unless the value is the predeclared identifier nil, +which has no type). +The dynamic type may vary during execution but values stored in interface +variables are always assignable +to the static type of the variable. +

+ +
+var x interface{}  // x is nil and has static type interface{}
+var v *T           // v has value nil, static type *T
+x = 42             // x has value 42 and dynamic type int
+x = v              // x has value (*T)(nil) and dynamic type *T
+
+ +

+A variable's value is retrieved by referring to the variable in an +expression; it is the most recent value +assigned to the variable. +If a variable has not yet been assigned a value, its value is the +zero value for its type. +

+ +

Types

@@ -672,17 +731,6 @@ interface, slice, map, and channel types—may be constructed using type literals.

-

-The static type (or just type) of a variable is the -type defined by its declaration. Variables of interface type -also have a distinct dynamic type, which -is the actual type of the value stored in the variable at run time. -The dynamic type may vary during execution but is always -assignable -to the static type of the interface variable. For non-interface -types, the dynamic type is always the static type. -

-

Each type T has an underlying type: If T is one of the predeclared boolean, numeric, or string types, or a type literal, @@ -1038,7 +1086,7 @@ struct {

Pointer types

-A pointer type denotes the set of all pointers to variables of a given +A pointer type denotes the set of all pointers to variables of a given type, called the base type of the pointer. The value of an uninitialized pointer is nil.

@@ -1461,7 +1509,7 @@ is different from []string.

Assignability

-A value x is assignable to a variable of type T +A value x is assignable to a variable of type T ("x is assignable to T") in any of these cases:

@@ -2266,7 +2314,8 @@ For array and slice literals the following rules apply:

Taking the address of a composite literal -generates a pointer to a unique instance of the literal's value. +generates a pointer to a unique variable initialized +with the literal's value.

 var pointer *Point3D = &Point3D{y: 1000}
@@ -3628,7 +3677,7 @@ then the evaluation of &x does too.
 
 

For an operand x of pointer type *T, the pointer -indirection *x denotes the value of type T pointed +indirection *x denotes the variable of type T pointed to by x. If x is nil, an attempt to evaluate *x will cause a run-time panic. @@ -5405,9 +5454,11 @@ var z complex128

Allocation

-The built-in function new takes a type T and -returns a value of type *T. -The memory is initialized as described in the section on +The built-in function new takes a type T, +allocates storage for a variable of that type +at run time, and returns a value of type *T +pointing to it. +The variable is initialized as described in the section on initial values.

@@ -5425,10 +5476,10 @@ new(S)

-dynamically allocates memory for a variable of type S, +allocates storage for a variable of type S, initializes it (a=0, b=0.0), and returns a value of type *S containing the address -of the memory. +of the location.

Making slices, maps and channels

@@ -5895,10 +5946,12 @@ func main() {

The zero value

-When memory is allocated to store a value, either through a declaration -or a call of make or new, -and no explicit initialization is provided, the memory is -given a default initialization. Each element of such a value is +When storage is allocated for a variable, +either through a declaration or a call of new, or when +a new value is created, either through a composite literal or a call +of make, +and no explicit initialization is provided, the variable or value is +given a default value. Each element of such a variable or value is set to the zero value for its type: false for booleans, 0 for integers, 0.0 for floats, "" for strings, and nil for pointers, functions, interfaces, slices, channels, and maps. -- cgit v1.2.1