From 56809d0ade51d3bbd653ba9e9b7c54e2f4ec5f66 Mon Sep 17 00:00:00 2001
From: Robert Griesemer
-A type determines the set of values and operations specific to values of that type.
-A type may be specified by a (possibly qualified (§Qualified identifiers))
-type name (§Type declarations) or a type literal,
-which composes a new type in terms of previously declared types.
+A type determines the set of values and operations specific to values of that
+type. A type may be specified by a (possibly qualified) type name
+(§Qualified identifier, §Type declarations) or a type literal,
+which composes a new type from previously declared types.
Types
@@ -450,10 +449,16 @@ because the size of the pointer itself is always known.
interface fit in here.)
-The interface of a type is the set of methods bound to it
-(§Method declarations); for pointer types, it is the interface
-of the pointer base type (§Pointer types). All types have an interface;
-if they have no methods, it is the empty interface.
+A type may have a method set associated with it
+(§Interface types, §Method declarations).
+The method set of an interface type (§Interface types) is its interface.
+The method set of any other named type T
+consists of all methods with receiver
+type T
.
+The method set of the corresponding pointer type *T
+is the set of all methods with receiver *T
or T
+(that is, it also contains the method set of T
).
+Any other type has an empty method set.
The static type (or just type) of a variable is the @@ -461,7 +466,7 @@ type defined by its declaration. Variables of interface type (§Interface types) 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 compatible -with the static type of the interface variable. For non-interfaces +with the static type of the interface variable. For non-interface types, the dynamic type is always the static type.
@@ -736,10 +741,28 @@ struct {
Fields and methods (§Method declarations) of an anonymous field are
promoted to be ordinary fields and methods of the struct (§Selectors).
+The following rules apply for a struct type named S
and
+a type named T
:
S
contains an anonymous field T
, the
+ method set of S
includes the method set of T
.
+ S
contains an anonymous field *T
, the
+ method set of S
includes the method set of *T
+ (which itself includes the method set of T
).
+ S
contains an anonymous field T
or
+ *T
, the method set of *S
includes the
+ method set of *T
(which itself includes the method
+ set of T
).
+ -A field declaration may be followed by an optional string literal tag, which -becomes an attribute for all the identifiers in the corresponding +A field declaration may be followed by an optional string literal tag, +which becomes an attribute for all the identifiers in the corresponding field declaration. The tags are made visible through a reflection library (TODO: reference?) but are otherwise ignored. @@ -824,10 +847,10 @@ func (n int) (func (p* T))
-An interface type specifies an unordered set of methods. A variable
-of interface type can store, dynamically, any value that implements
-at least that set of methods.
-An interface value may be nil
.
+An interface type specifies a method set called its interface.
+A variable of interface type can store a value of any type with a method set
+that is any superset of the interface. Such a type is said to
+implement the interface. An interface value may be nil
.
@@ -846,11 +869,9 @@ interface {
-Any type (including interface types) whose interface includes,
-possibly as a subset, the complete set of methods of an interface I
-is said to implement interface I
.
+More than one type may implement an interface.
For instance, if two types S1
and S2
-have the methods
+have the method set
@@ -1066,7 +1087,7 @@ identical types. In detail:
-The receiver type must be a type name or a pointer to a type name,
-and that name is called the receiver base type or just base type.
-The base type must not be a pointer type and must be
+The receiver type must be of the form T
or *T
where
+T
is a type name. T
is called the
+receiver base type or just base type.
+The base type must not be a pointer or interface type and must be
declared in the same source file as the method.
The method is said to be bound to the base type
and is visible only within selectors for that type
@@ -1630,8 +1652,6 @@ and is visible only within selectors for that type
-All methods bound to a base type must have the same receiver type,
-either all pointers to the base type or all the base type itself.
Given type Point
, the declarations
Point
.
-If the -receiver's value is not referenced inside the the body of the method, +If the receiver's value is not referenced inside the the body of the method, its identifier may be omitted in the declaration. The same applies in general to parameters of functions and methods.
@@ -1723,8 +1742,8 @@ func F(a int) int {An expression specifies the computation of a value by applying -operators and functions to operands. An expression has a value and -a type. +operators and functions to operands. An expression has a value +and a type.
-If the receiver type of the method is declared as a pointer of type *T
,
-the actual receiver may be a value of type T
;
-in such cases method invocation implicitly takes the
-receiver's address:
+A method call x.m()
is valid if the method set of
+(the type of) x
contains m
(and the
+argument list is compatible with the parameter list of m
).
+If x
is addressable and &x
's method
+set contains m
, x.m()
is shorthand
+for (&x).m()
:
-- 2.48.1