From 164a7bceeb67af9490d970e3c6e665198c6e1c9a Mon Sep 17 00:00:00 2001
From: Robert Griesemer
-chan T // can be used to send and receive values of type T +chan T // can be used to send and receive values of type T chan<- float // can only be used to send floats <-chan int // can only be used to receive ints@@ -1121,12 +1118,8 @@ or absent, the communication succeeds only when both a sender and receiver are r
-For a channel c
, the predefined function close(c)
-marks the channel as unable to accept more
-values through a send operation. After any previously
-sent values have been received, receives will return
-the zero value for the channel's type. After at least one such zero value has been
-received, closed(c)
returns true.
+A channel may be closed and tested for closure with the built-in functions
+close
and closed
.
CompositeLit = LiteralType "{" [ ElementList ] "}" . LiteralType = StructType | ArrayType | "[" "..." "]" ElementType | - SliceType | MapType | TypeName . + SliceType | MapType | TypeName | "(" LiteralType ")" . ElementList = Element { "," Element } [ "," ] . Element = [ Key ":" ] Value . Key = FieldName | Index . @@ -2134,10 +2127,15 @@ as they are accessible.Primary expressions
++Primary expressions are the operands for unary and binary expressions. +
+PrimaryExpr = Operand | Conversion | + BuiltinCall | PrimaryExpr Selector | PrimaryExpr Index | PrimaryExpr Slice | @@ -3059,6 +3057,84 @@ It is legal to derive a function value from a method of an interface type. The resulting function takes an explicit receiver of that interface type. +Conversions
+ ++Conversions are expressions of the form
+ +T(x)
+whereT
is a type andx
is an expression +that can be converted to typeT
. ++Conversion = LiteralType "(" Expression ")" . ++ ++The following conversion rules apply: +
+
T
.
+T
if the value's type, or T
, or any of their component
+types were unnamed.
+x := uint16(0x10F0)
, then uint32(int8(x)) == 0xFFFFFFF0
.
+The conversion always yields a valid value; there is no indication of overflow.
++string(0x65e5) // "\u65e5" == "日" == "\xe6\x97\xa5" ++ +
nil
, the result is the empty string.
++string([]int{0x767d, 0x9d6c, 0x7fd4}) // "\u767d\u9d6c\u7fd4" == "白鵬翔"+
nil
,
+the result is the empty string.
+
++string([]byte{'h', 'e', 'l', 'l', 'o'}) // "hello" ++
+There is no linguistic mechanism to convert between pointers and integers.
+The package unsafe
+implements this functionality under
+restricted circumstances.
+
@@ -3978,30 +4054,47 @@ for i := 0; i <= 3; i++ { } -
+A small number of built-in functions are +predeclared. +They are called like any other function but some of them +accept a type instead of an expression as the first argument. +
+ ++BuiltinCall = identifier "(" [ BuiltinArgs ] ")" . +BuiltinArgs = Type [ "," ExpressionList ] | ExpressionList . ++ +
+For a channel c
, the predefined function close(c)
+marks the channel as unable to accept more
+values through a send operation. After any previously
+sent values have been received, receive operations will return
+the zero value for the channel's type. After at least one such zero value has been
+received, closed(c)
returns true.
+
+The built-in functions len
and cap
take arguments
+of various types and return a result of type int
.
+The implementation guarantees that the result always fits into an int
.
+
Call Argument type Result -len(s) string string length (in bytes) +len(s) string type string length in bytes [n]T, *[n]T array length (== n) []T slice length map[K]T map length (number of defined keys) - chan T number of elements sent queued in channel buffer + chan T number of elements queued in channel buffer cap(s) [n]T, *[n]T array length (== n) []T slice capacity @@ -4009,107 +4102,16 @@ cap(s) [n]T, *[n]T array length (== n)
-The type of the result is always int
and the
-implementation guarantees that
-the result always fits into an int
.
-
-The capacity of a slice or map is the number of elements for which there is
-space allocated in the underlying array (for a slice) or map. For a slice
-s
, at any time the following relationship holds:
+The capacity of a slice is the number of elements for which there is
+space allocated in the underlying array.
+At any time the following relationship holds:
+
0 <= len(s) <= cap(s)-
-Conversions look like function calls of the form -
- --T(value) -- -
-where T
is a type
-and value
is an expression
-that can be converted to a value
-of result type T
.
-
-Conversion = ( TypeName | "(" Type ")" ) Expression . -- -
-The following conversion rules apply: -
-T
.
-T
if the value's type, or T
, or any of their component
-types were unnamed (§Type identity and compatibility).
-x := uint16(0x10F0)
, then uint32(int8(x)) == 0xFFFFFFF0
.
-The conversion always yields a valid value; there is no indication of overflow.
--string(0x65e5) // "\u65e5" == "日" == "\xe6\x97\xa5" -- -
nil
, the result is the empty string.
--string([]int{0x767d, 0x9d6c, 0x7fd4}) // "\u767d\u9d6c\u7fd4" == "白鵬翔"-
nil
,
-the result is the empty string.
-
--string([]byte{'h', 'e', 'l', 'l', 'o'}) // "hello" --
-There is no linguistic mechanism to convert between pointers and integers.
-The package unsafe
-implements this functionality under
-restricted circumstances.
-
@@ -4180,10 +4182,25 @@ c := make(chan int, 10); # channel with a buffer size of 10 m := make(map[string] int, 100); # map with initial space for 100 elements + +
-TODO: Need syntax that permits a type as first argument for built-ins. +Current implementations provide several built-in functions useful during +bootstrapping. These functions are documented for completeness but are not +guaranteed to stay in the language. They do not return a result.
++Call Behavior + +print prints all arguments; formatting of arguments is implementation-specific +println like print but prints spaces between arguments and a newline at the end +panic like print, aborts execution after printing +panicln like println, aborts execution after printing ++ +