-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspec.tex
4563 lines (3690 loc) · 153 KB
/
spec.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\cleardoublepage
\phantomsection
\addcontentsline{toc}{chapter}{Go Language Specification}
\appendix
\chapter*{Go Language Specification}
\section*{Introduction}
This is a reference manual for the Go programming language. For more
information and other documents, see
\href{http://golang.org/}{http://golang.org}.
Go is a general-purpose language designed with systems programming in
mind. It is strongly typed and garbage-collected and has explicit
support for concurrent programming. Programs are constructed from
\emph{packages}, whose properties allow efficient management of
dependencies. The existing implementations use a traditional
compile/link model to generate executable binaries.
The grammar is compact and regular, allowing for easy analysis by
automatic tools such as integrated development environments.
\section*{Notation}
The syntax is specified using Extended Backus-Naur Form (EBNF):
\begin{Verbatim}[frame=single]
Production = production_name "=" [ Expression ] "." .
Expression = Alternative { "|" Alternative } .
Alternative = Term { Term } .
Term = production_name | token [ "…" token ] | Group | Option | Repetition .
Group = "(" Expression ")" .
Option = "[" Expression "]" .
Repetition = "{" Expression "}" .
\end{Verbatim}
Productions are expressions constructed from terms and the following
operators, in increasing precedence:
\begin{Verbatim}[frame=single]
| alternation
() grouping
[] option (0 or 1 times)
{} repetition (0 to n times)
\end{Verbatim}
Lower-case production names are used to identify lexical tokens.
Non-terminals are in CamelCase. Lexical tokens are enclosed in double
quotes \texttt{""} or back quotes \texttt{``}.
The form \texttt{a \ldots{} b} represents the set of characters from
\texttt{a} through \texttt{b} as alternatives. The horizontal ellipsis
\texttt{\ldots{}} is also used elsewhere in the spec to informally
denote various enumerations or code snippets that are not further
specified. The character \texttt{\ldots{}} (as opposed to the three
characters \texttt{...}) is not a token of the Go language.
\section*{Source code representation}
Source code is Unicode text encoded in
\href{http://en.wikipedia.org/wiki/UTF-8}{UTF-8}. The text is not
canonicalized, so a single accented code point is distinct from the same
character constructed from combining an accent and a letter; those are
treated as two code points. For simplicity, this document will use the
unqualified term \emph{character} to refer to a Unicode code point in
the source text.
Each code point is distinct; for instance, upper and lower case letters
are different characters.
Implementation restriction: For compatibility with other tools, a
compiler may disallow the NUL character (U+0000) in the source text.
\subsection*{Characters}
The following terms are used to denote specific Unicode character
classes:
\begin{Verbatim}[frame=single]
newline = /* the Unicode code point U+000A */ .
unicode_char = /* an arbitrary Unicode code point except newline */ .
unicode_letter = /* a Unicode code point classified as "Letter" */ .
unicode_digit = /* a Unicode code point classified as "Decimal Digit" */ .
\end{Verbatim}
In \href{http://www.unicode.org/versions/Unicode6.0.0/}{The Unicode
Standard 6.0}, Section 4.5 ``General Category'' defines a set of
character categories. Go treats those characters in category Lu, Ll, Lt,
Lm, or Lo as Unicode letters, and those in category Nd as Unicode
digits.
\subsection*{Letters and digits}
The underscore character \texttt{\_} (U+005F) is considered a letter.
\begin{Verbatim}[frame=single]
letter = unicode_letter | "_" .
decimal_digit = "0" … "9" .
octal_digit = "0" … "7" .
hex_digit = "0" … "9" | "A" … "F" | "a" … "f" .
\end{Verbatim}
\section*{Lexical elements}
\subsection*{Comments}
There are two forms of comments:
\begin{enumerate}
\item
\emph{Line comments} start with the character sequence \texttt{//} and
stop at the end of the line. A line comment acts like a newline.
\item
\emph{General comments} start with the character sequence \texttt{/*}
and continue through the character sequence \texttt{*/}. A general
comment containing one or more newlines acts like a newline, otherwise
it acts like a space.
\end{enumerate}
Comments do not nest.
\subsection*{Tokens}
Tokens form the vocabulary of the Go language. There are four classes:
\emph{identifiers}, \emph{keywords}, \emph{operators and delimiters},
and \emph{literals}. \emph{White space}, formed from spaces (U+0020),
horizontal tabs (U+0009), carriage returns (U+000D), and newlines
(U+000A), is ignored except as it separates tokens that would otherwise
combine into a single token. Also, a newline or end of file may trigger
the insertion of a semicolon. While breaking the
input into tokens, the next token is the longest sequence of characters
that form a valid token.
\subsection*{Semicolons}
The formal grammar uses semicolons \texttt{";"} as terminators in a
number of productions. Go programs may omit most of these semicolons
using the following two rules:
\begin{enumerate}
\item
When the input is broken into tokens, a semicolon is automatically
inserted into the token stream at the end of a non-blank line if the
line's final token is
\begin{itemize}
\item
an identifier
\item
an integer,
floating-point,
imaginary,
rune, or
string literal
\item
one of the keywords \texttt{break},
\texttt{continue}, \texttt{fallthrough}, or \texttt{return}
\item
one of the operators and delimiters \texttt{++}, \texttt{-{}-},
\texttt{)}, \texttt{]}, or \texttt{\}}
\end{itemize}
\item
To allow complex statements to occupy a single line, a semicolon may
be omitted before a closing \texttt{")"} or \texttt{"\}"}.
\end{enumerate}
To reflect idiomatic use, code examples in this document elide
semicolons using these rules.
\subsection*{Identifiers}
Identifiers name program entities such as variables and types. An
identifier is a sequence of one or more letters and digits. The first
character in an identifier must be a letter.
\begin{Verbatim}[frame=single]
identifier = letter { letter | unicode_digit } .
\end{Verbatim}
\begin{Verbatim}[frame=single]
a
_x9
ThisVariableIsExported
αβ
\end{Verbatim}
Some identifiers are predeclared.
\subsection*{Keywords}
The following keywords are reserved and may not be used as identifiers.
\begin{Verbatim}[frame=single]
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
\end{Verbatim}
\subsection*{Operators and Delimiters}
The following character sequences represent
operators, delimiters, and other special tokens:
\begin{Verbatim}[frame=single]
+ & += &= && == != ( )
- | -= |= || < <= [ ]
* ^ *= ^= <- > >= { }
/ << /= <<= ++ = := , ;
% >> %= >>= -- ! ... . :
&^ &^=
\end{Verbatim}
\subsection*{Integer literals}
An integer literal is a sequence of digits representing an
integer constant. An optional prefix sets a
non-decimal base: \texttt{0} for octal, \texttt{0x} or \texttt{0X} for
hexadecimal. In hexadecimal literals, letters \texttt{a-f} and
\texttt{A-F} represent values 10 through 15.
\begin{Verbatim}[frame=single]
int_lit = decimal_lit | octal_lit | hex_lit .
decimal_lit = ( "1" … "9" ) { decimal_digit } .
octal_lit = "0" { octal_digit } .
hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit } .
\end{Verbatim}
\begin{Verbatim}[frame=single]
42
0600
0xBadFace
170141183460469231731687303715884105727
\end{Verbatim}
\subsection*{Floating-point literals}
A floating-point literal is a decimal representation of a
floating-point constant. It has an integer part, a
decimal point, a fractional part, and an exponent part. The integer and
fractional part comprise decimal digits; the exponent part is an
\texttt{e} or \texttt{E} followed by an optionally signed decimal
exponent. One of the integer part or the fractional part may be elided;
one of the decimal point or the exponent may be elided.
\begin{Verbatim}[frame=single]
float_lit = decimals "." [ decimals ] [ exponent ] |
decimals exponent |
"." decimals [ exponent ] .
decimals = decimal_digit { decimal_digit } .
exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
\end{Verbatim}
\begin{Verbatim}[frame=single]
0.
72.40
072.40 // == 72.40
2.71828
1.e+0
6.67428e-11
1E6
.25
.12345E+5
\end{Verbatim}
\subsection*{Imaginary literals}
An imaginary literal is a decimal representation of the imaginary part
of a complex constant. It consists of a
floating-point literal or decimal
integer followed by the lower-case letter \texttt{i}.
\begin{Verbatim}[frame=single]
imaginary_lit = (decimals | float_lit) "i" .
\end{Verbatim}
\begin{Verbatim}[frame=single]
0i
011i // == 11i
0.i
2.71828i
1.e+0i
6.67428e-11i
1E6i
.25i
.12345E+5i
\end{Verbatim}
\subsection*{Rune literals}
A rune literal represents a rune constant, an
integer value identifying a Unicode code point. A rune literal is
expressed as one or more characters enclosed in single quotes. Within
the quotes, any character may appear except single quote and newline. A
single quoted character represents the Unicode value of the character
itself, while multi-character sequences beginning with a backslash
encode values in various formats.
The simplest form represents the single character within the quotes;
since Go source text is Unicode characters encoded in UTF-8, multiple
UTF-8-encoded bytes may represent a single integer value. For instance,
the literal \texttt{'a'} holds a single byte representing a literal
\texttt{a}, Unicode U+0061, value \texttt{0x61}, while \texttt{'ä'}
holds two bytes (\texttt{0xc3} \texttt{0xa4}) representing a literal
\texttt{a}-dieresis, U+00E4, value \texttt{0xe4}.
Several backslash escapes allow arbitrary values to be encoded as ASCII
text. There are four ways to represent the integer value as a numeric
constant: \texttt{\textbackslash{}x} followed by exactly two hexadecimal
digits; \texttt{\textbackslash{}u} followed by exactly four hexadecimal
digits; \texttt{\textbackslash{}U} followed by exactly eight hexadecimal
digits, and a plain backslash \texttt{\textbackslash{}} followed by
exactly three octal digits. In each case the value of the literal is the
value represented by the digits in the corresponding base.
Although these representations all result in an integer, they have
different valid ranges. Octal escapes must represent a value between 0
and 255 inclusive. Hexadecimal escapes satisfy this condition by
construction. The escapes \texttt{\textbackslash{}u} and
\texttt{\textbackslash{}U} represent Unicode code points so within them
some values are illegal, in particular those above \texttt{0x10FFFF} and
surrogate halves.
After a backslash, certain single-character escapes represent special
values:
\begin{Verbatim}[frame=single]
\a U+0007 alert or bell
\b U+0008 backspace
\f U+000C form feed
\n U+000A line feed or newline
\r U+000D carriage return
\t U+0009 horizontal tab
\v U+000b vertical tab
\\ U+005c backslash
\' U+0027 single quote (valid escape only within rune literals)
\" U+0022 double quote (valid escape only within string literals)
\end{Verbatim}
All other sequences starting with a backslash are illegal inside rune
literals.
\begin{Verbatim}[frame=single]
char_lit = "'" ( unicode_value | byte_value ) "'" .
unicode_value = unicode_char | little_u_value | big_u_value | escaped_char .
byte_value = octal_byte_value | hex_byte_value .
octal_byte_value = `\` octal_digit octal_digit octal_digit .
hex_byte_value = `\` "x" hex_digit hex_digit .
little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit .
big_u_value = `\` "U" 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" | `\` | "'" | `"` ) .
\end{Verbatim}
\begin{Verbatim}[frame=single]
'a'
'ä'
'本'
'\t'
'\000'
'\007'
'\377'
'\x07'
'\xff'
'\u12e4'
'\U00101234'
'aa' // illegal: too many characters
'\xa' // illegal: too few hexadecimal digits
'\0' // illegal: too few octal digits
'\uDFFF' // illegal: surrogate half
'\U00110000' // illegal: invalid Unicode code point
\end{Verbatim}
\subsection*{String literals}
A string literal represents a string constant
obtained from concatenating a sequence of characters. There are two
forms: raw string literals and interpreted string literals.
Raw string literals are character sequences between back quotes
\texttt{``}. Within the quotes, any character is legal except back
quote. The value of a raw string literal is the string composed of the
uninterpreted (implicitly UTF-8-encoded) characters between the quotes;
in particular, backslashes have no special meaning and the string may
contain newlines. Carriage returns inside raw string literals are
discarded from the raw string value.
Interpreted string literals are character sequences between double
quotes \texttt{""}. The text between the quotes, which may not contain
newlines, forms the value of the literal, with backslash escapes
interpreted as they are in rune literals (except that
\texttt{\textbackslash{}'} is illegal and \texttt{\textbackslash{}"} is
legal), with the same restrictions. The three-digit octal
(\texttt{\textbackslash{}}\emph{nnn}) and two-digit hexadecimal
(\texttt{\textbackslash{}x}\emph{nn}) escapes represent individual
\emph{bytes} of the resulting string; all other escapes represent the
(possibly multi-byte) UTF-8 encoding of individual \emph{characters}.
Thus inside a string literal \texttt{\textbackslash{}377} and
\texttt{\textbackslash{}xFF} represent a single byte of value
\texttt{0xFF}=255, while \texttt{ÿ}, \texttt{\textbackslash{}u00FF},
\texttt{\textbackslash{}U000000FF} and
\texttt{\textbackslash{}xc3\textbackslash{}xbf} represent the two bytes
\texttt{0xc3} \texttt{0xbf} of the UTF-8 encoding of character U+00FF.
\begin{Verbatim}[frame=single]
string_lit = raw_string_lit | interpreted_string_lit .
raw_string_lit = "`" { unicode_char | newline } "`" .
interpreted_string_lit = `"` { unicode_value | byte_value } `"` .
\end{Verbatim}
\begin{Verbatim}[frame=single]
`abc` // same as "abc"
`\n
\n` // same as "\\n\n\\n"
"\n"
""
"Hello, world!\n"
"日本語"
"\u65e5本\U00008a9e"
"\xff\u00FF"
"\uD800" // illegal: surrogate half
"\U00110000" // illegal: invalid Unicode code point
\end{Verbatim}
These examples all represent the same string:
\begin{Verbatim}[frame=single]
"日本語" // UTF-8 input text
`日本語` // UTF-8 input text as a raw literal
"\u65e5\u672c\u8a9e" // the explicit Unicode code points
"\U000065e5\U0000672c\U00008a9e" // the explicit Unicode code points
"\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // the explicit UTF-8 bytes
\end{Verbatim}
If the source code represents a character as two code points, such as a
combining form involving an accent and a letter, the result will be an
error if placed in a rune literal (it is not a single code point), and
will appear as two code points if placed in a string literal.
\section*{Constants}
There are \emph{boolean constants}, \emph{rune constants}, \emph{integer
constants}, \emph{floating-point constants}, \emph{complex constants},
and \emph{string constants}. Character, integer, floating-point, and
complex constants are collectively called \emph{numeric constants}.
A constant value is represented by a rune, integer, floating-point,
imaginary, or string literal, an identifier denoting a constant, a
constant expression, a conversion with a result that is a constant,
or the result value of some built-in functions such as
\texttt{unsafe.Sizeof} applied to any value, \texttt{cap} or
\texttt{len} applied to some expressions, \texttt{real} and
\texttt{imag} applied to a complex constant and \texttt{complex}
applied to numeric constants. The boolean truth values are represented
by the predeclared constants \texttt{true} and \texttt{false}. The
predeclared identifier iota denotes an integer constant.
In general, complex constants are a form of
constant expression and are discussed
in that section.
Numeric constants represent values of arbitrary precision and do not
overflow.
Constants may be typed or untyped. Literal constants,
\texttt{true}, \texttt{false}, \texttt{iota}, and certain
constant expressions containing only
untyped constant operands are untyped.
A constant may be given a type explicitly by a
constant declaration or
conversion, or implicitly when used in a
variable declaration or an
assignment or as an operand in an
expression. It is an error if the constant value
cannot be represented as a value of the respective type. For instance,
\texttt{3.0} can be given any integer or any floating-point type, while
\texttt{2147483648.0} (equal to \texttt{1\textless{}\textless{}31}) can
be given the types \texttt{float32}, \texttt{float64}, or
\texttt{uint32} but not \texttt{int32} or \texttt{string}.
There are no constants denoting the IEEE-754 infinity and not-a-number
values, but the \href{/pkg/math/}{\texttt{math} package}'s
\href{/pkg/math/\#Inf}{Inf}, \href{/pkg/math/\#NaN}{NaN},
\href{/pkg/math/\#IsInf}{IsInf}, and \href{/pkg/math/\#IsNaN}{IsNaN}
functions return and test for those values at run time.
Implementation restriction: Although numeric constants have arbitrary
precision in the language, a compiler may implement them using an
internal representation with limited precision. That said, every
implementation must:
\begin{itemize}
\item
Represent integer constants with at least 256 bits.
\item
Represent floating-point constants, including the parts of a complex
constant, with a mantissa of at least 256 bits and a signed exponent
of at least 32 bits.
\item
Give an error if unable to represent an integer constant precisely.
\item
Give an error if unable to represent a floating-point or complex
constant due to overflow.
\item
Round to the nearest representable constant if unable to represent a
floating-point or complex constant due to limits on precision.
\end{itemize}
These requirements apply both to literal constants and to the result of
evaluating constant expressions.
\section*{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)
\emph{type name} a \emph{type literal}, which composes a new type
from previously declared types.
\begin{Verbatim}[frame=single]
Type = TypeName | TypeLit | "(" Type ")" .
TypeName = identifier | QualifiedIdent .
TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType |
SliceType | MapType | ChannelType .
\end{Verbatim}
Named instances of the boolean, numeric, and string types are
predeclared. \emph{Composite
types}---array, struct, pointer, function, interface, slice, map, and
channel types---may be constructed using type literals.
The \emph{static type} (or just \emph{type}) of a variable is the type
defined by its declaration. Variables of interface type also have a
distinct \emph{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 \texttt{T} has an \emph{underlying type}: If \texttt{T} is a
predeclared type or a type literal, the corresponding underlying type is
\texttt{T} itself. Otherwise, \texttt{T}'s underlying type is the
underlying type of the type to which \texttt{T} refers in its
type declaration.
\begin{Verbatim}[frame=single]
type T1 string
type T2 T1
type T3 []T1
type T4 T3
\end{Verbatim}
The underlying type of \texttt{string}, \texttt{T1}, and \texttt{T2} is
\texttt{string}. The underlying type of \texttt{{[}{]}T1}, \texttt{T3},
and \texttt{T4} is \texttt{{[}{]}T1}.
\subsection*{Method sets}
A type may have a \emph{method set} associated with it. The method
set of an interface type is its interface. The method set of any
other type \texttt{T} consists of all methods with receiver type
\texttt{T}. The method set of the corresponding pointer type
\texttt{*T} is the set of all methods with receiver \texttt{*T} or
\texttt{T} (that is, it also contains the method set of \texttt{T}).
Further rules apply to structs containing anonymous fields, as
described in the section on struct types. Any other type has an
empty method set. In a method set, each method must have a unique
method name.
The method set of a type determines the interfaces that the type
implements and the methods that can be
called using a receiver of that type.
\subsection*{Boolean types}
A \emph{boolean type} represents the set of Boolean truth values denoted
by the predeclared constants \texttt{true} and \texttt{false}. The
predeclared boolean type is \texttt{bool}.
\subsection*{Numeric types}
A \emph{numeric type} represents sets of integer or floating-point
values. The predeclared architecture-independent numeric types are:
\begin{Verbatim}[frame=single]
uint8 the set of all unsigned 8-bit integers (0 to 255)
uint16 the set of all unsigned 16-bit integers (0 to 65535)
uint32 the set of all unsigned 32-bit integers (0 to 4294967295)
uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615)
int8 the set of all signed 8-bit integers (-128 to 127)
int16 the set of all signed 16-bit integers (-32768 to 32767)
int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
float32 the set of all IEEE-754 32-bit floating-point numbers
float64 the set of all IEEE-754 64-bit floating-point numbers
complex64 the set of all complex numbers with float32 real and imaginary parts
complex128 the set of all complex numbers with float64 real and imaginary parts
byte alias for uint8
rune alias for int32
\end{Verbatim}
The value of an \emph{n}-bit integer is \emph{n} bits wide and
represented using
\href{http://en.wikipedia.org/wiki/Two's\_complement}{two's complement
arithmetic}.
There is also a set of predeclared numeric types with
implementation-specific sizes:
\begin{Verbatim}[frame=single]
uint either 32 or 64 bits
int same size as uint
uintptr an unsigned integer large enough to store the uninterpreted bits of a pointer value
\end{Verbatim}
To avoid portability issues all numeric types are distinct except
\texttt{byte}, which is an alias for \texttt{uint8}, and \texttt{rune},
which is an alias for \texttt{int32}. Conversions are required when
different numeric types are mixed in an expression or assignment. For
instance, \texttt{int32} and \texttt{int} are not the same type even
though they may have the same size on a particular architecture.
\subsection*{String types}
A \emph{string type} represents the set of string values. Strings behave
like slices of bytes but are immutable: once created, it is impossible
to change the contents of a string. The predeclared string type is
\texttt{string}.
The elements of strings have type \texttt{byte} and may be accessed
using the usual indexing operations. It is illegal
to take the address of such an element; if \texttt{s{[}i{]}} is the
\emph{i}th byte of a string, \texttt{\&s{[}i{]}} is invalid. The length
of string \texttt{s} can be discovered using the built-in function
\texttt{len}. The length is a compile-time constant if \texttt{s} is a
string literal.
\subsection*{Array types}
An array is a numbered sequence of elements of a single type, called the
element type. The number of elements is called the length and is never
negative.
\begin{Verbatim}[frame=single]
ArrayType = "[" ArrayLength "]" ElementType .
ArrayLength = Expression .
ElementType = Type .
\end{Verbatim}
The length is part of the array's type and must be a constant
expression that evaluates to a non-negative integer value. The
length of array \texttt{a} can be discovered using the built-in
function \texttt{len(a)}. The elements can be indexed by integer
indices 0 through \texttt{len(a)-1}. Array types are always
one-dimensional but may be composed to form multi-dimensional types.
\begin{Verbatim}[frame=single]
[32]byte
[2*N] struct { x, y int32 }
[1000]*float64
[3][5]int
[2][2][2]float64 // same as [2]([2]([2]float64))
\end{Verbatim}
\subsection*{Slice types}
A slice is a reference to a contiguous segment of an array and contains
a numbered sequence of elements from that array. A slice type denotes
the set of all slices of arrays of its element type. The value of an
uninitialized slice is \texttt{nil}.
\begin{Verbatim}[frame=single]
SliceType = "[" "]" ElementType .
\end{Verbatim}
Like arrays, slices are indexable and have a length. The length of
a slice \texttt{s} can be discovered by the built-in function
\texttt{len(s)}; unlike with arrays it may change during execution.
The elements can be addressed by integer indices 0 through
\texttt{len(s)-1}. The slice index of a given element may be less
than the index of the same element in the underlying array.
A slice, once initialized, is always associated with an underlying array
that holds its elements. A slice therefore shares storage with its array
and with other slices of the same array; by contrast, distinct arrays
always represent distinct storage.
The array underlying a slice may extend past the end of the slice.
The \emph{capacity} is a measure of that extent: it is the sum of
the length of the slice and the length of the array beyond the
slice; a slice of length up to that capacity can be created by
`slicing' a new one from the original slice. The capacity of a slice
\texttt{a} can be discovered using the built-in function \texttt{cap(a)}.
A new, initialized slice value for a given element type \texttt{T} is
made using the built-in function
\texttt{make}, which
takes a slice type and parameters specifying the length and optionally
the capacity:
\begin{Verbatim}[frame=single]
make([]T, length)
make([]T, length, capacity)
\end{Verbatim}
A call to \texttt{make} allocates a new, hidden array to which the
returned slice value refers. That is, executing
\begin{Verbatim}[frame=single]
make([]T, length, capacity)
\end{Verbatim}
produces the same slice as allocating an array and slicing it, so these
two examples result in the same slice:
\begin{Verbatim}[frame=single]
make([]int, 50, 100)
new([100]int)[0:50]
\end{Verbatim}
Like arrays, slices are always one-dimensional but may be composed to
construct higher-dimensional objects. With arrays of arrays, the inner
arrays are, by construction, always the same length; however with slices
of slices (or arrays of slices), the lengths may vary dynamically.
Moreover, the inner slices must be allocated individually (with
\texttt{make}).
\subsection*{Struct types}
A struct is a sequence of named elements, called fields, each of which
has a name and a type. Field names may be specified explicitly
(IdentifierList) or implicitly (AnonymousField). Within a struct,
non-blank field names must be
unique.
\begin{Verbatim}[frame=single]
StructType = "struct" "{" { FieldDecl ";" } "}" .
FieldDecl = (IdentifierList Type | AnonymousField) [ Tag ] .
AnonymousField = [ "*" ] TypeName .
Tag = string_lit .
\end{Verbatim}
\begin{Verbatim}[frame=single]
// An empty struct.
struct {}
// A struct with 6 fields.
struct {
x, y int
u float32
_ float32 // padding
A *[]int
F func()
}
\end{Verbatim}
A field declared with a type but no explicit field name is an
\emph{anonymous field}, also called an \emph{embedded} field or an
embedding of the type in the struct. An embedded type must be specified
as a type name \texttt{T} or as a pointer to a non-interface type name
\texttt{*T}, and \texttt{T} itself may not be a pointer type. The
unqualified type name acts as the field name.
\begin{Verbatim}[frame=single]
// A struct with four anonymous fields of type T1, *T2, P.T3 and *P.T4
struct {
T1 // field name is T1
*T2 // field name is T2
P.T3 // field name is T3
*P.T4 // field name is T4
x, y int // field names are x and y
}
\end{Verbatim}
The following declaration is illegal because field names must be unique
in a struct type:
\begin{Verbatim}[frame=single]
struct {
T // conflicts with anonymous field *T and *P.T
*T // conflicts with anonymous field T and *P.T
*P.T // conflicts with anonymous field T and *T
}
\end{Verbatim}
A field or method \texttt{f} of an
anonymous field in a struct \texttt{x} is called \emph{promoted} if
\texttt{x.f} is a legal selector that denotes that
field or method \texttt{f}.
Promoted fields act like ordinary fields of a struct except that they
cannot be used as field names in
composite literals of the struct.
Given a struct type \texttt{S} and a type named \texttt{T}, promoted
methods are included in the method set of the struct as follows:
\begin{itemize}
\item
If \texttt{S} contains an anonymous field \texttt{T}, the
method sets of \texttt{S} and \texttt{*S}
both include promoted methods with receiver \texttt{T}. The method set
of \texttt{*S} also includes promoted methods with receiver
\texttt{*T}.
\item
If \texttt{S} contains an anonymous field \texttt{*T}, the method sets
of \texttt{S} and \texttt{*S} both include promoted methods with
receiver \texttt{T} or \texttt{*T}.
\end{itemize}
A field declaration may be followed by an optional string literal
\emph{tag}, which becomes an attribute for all the fields in the
corresponding field declaration. The tags are made visible through a
reflection interface but are otherwise
ignored.
\begin{Verbatim}[frame=single]
// A struct corresponding to the TimeStamp protocol buffer.
// The tag strings define the protocol buffer field numbers.
struct {
microsec uint64 "field 1"
serverIP6 uint64 "field 2"
process string "field 3"
}
\end{Verbatim}
\subsection*{Pointer types}
A pointer type denotes the set of all pointers to variables of a given
type, called the \emph{base type} of the pointer. The value of an
uninitialized pointer is \texttt{nil}.
\begin{Verbatim}[frame=single]
PointerType = "*" BaseType .
BaseType = Type .
\end{Verbatim}
\begin{Verbatim}[frame=single]
*Point
*[4]int
\end{Verbatim}
\subsection*{Function types}
A function type denotes the set of all functions with the same parameter
and result types. The value of an uninitialized variable of function
type is \texttt{nil}.
\begin{Verbatim}[frame=single]
FunctionType = "func" Signature .
Signature = Parameters [ Result ] .
Result = Parameters | Type .
Parameters = "(" [ ParameterList [ "," ] ] ")" .
ParameterList = ParameterDecl { "," ParameterDecl } .
ParameterDecl = [ IdentifierList ] [ "..." ] Type .
\end{Verbatim}
Within a list of parameters or results, the names (IdentifierList) must
either all be present or all be absent. If present, each name stands for
one item (parameter or result) of the specified type; if absent, each
type stands for one item of that type. Parameter and result lists are
always parenthesized except that if there is exactly one unnamed result
it may be written as an unparenthesized type.
The final parameter in a function signature may have a type prefixed
with \texttt{...}. A function with such a parameter is called
\emph{variadic} and may be invoked with zero or more arguments for that
parameter.
\begin{Verbatim}[frame=single]
func()
func(x int) int
func(a, _ int, z float32) bool
func(a, b int, z float32) (bool)
func(prefix string, values ...int)
func(a, b int, z float64, opt ...interface{}) (success bool)
func(int, int, float64) (float64, *[]int)
func(n int) func(p *T)
\end{Verbatim}
\subsection*{Interface types}
An interface type specifies a method set called
its \emph{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 \emph{implement the interface}. The value of an
uninitialized variable of interface type is \texttt{nil}.
\begin{Verbatim}[frame=single]
InterfaceType = "interface" "{" { MethodSpec ";" } "}" .
MethodSpec = MethodName Signature | InterfaceTypeName .
MethodName = identifier .
InterfaceTypeName = TypeName .
\end{Verbatim}
As with all method sets, in an interface type, each method must have a
unique name.
\begin{Verbatim}[frame=single]
// A simple File interface
interface {
Read(b Buffer) bool
Write(b Buffer) bool
Close()
}
\end{Verbatim}
More than one type may implement an interface. For instance, if two
types \texttt{S1} and \texttt{S2} have the method set
\begin{Verbatim}[frame=single]
func (p T) Read(b Buffer) bool { return … }
func (p T) Write(b Buffer) bool { return … }
func (p T) Close() { … }
\end{Verbatim}
(where \texttt{T} stands for either \texttt{S1} or \texttt{S2}) then the
\texttt{File} interface is implemented by both \texttt{S1} and
\texttt{S2}, regardless of what other methods \texttt{S1} and
\texttt{S2} may have or share.
A type implements any interface comprising any subset of its methods and
may therefore implement several distinct interfaces. For instance, all
types implement the \emph{empty interface}:
\begin{Verbatim}[frame=single]
interface{}
\end{Verbatim}
Similarly, consider this interface specification, which appears within a
type declaration to define an interface
called \texttt{Lock}:
\begin{Verbatim}[frame=single]
type Lock interface {
Lock()
Unlock()
}
\end{Verbatim}
If \texttt{S1} and \texttt{S2} also implement
\begin{Verbatim}[frame=single]
func (p T) Lock() { … }
func (p T) Unlock() { … }
\end{Verbatim}
they implement the \texttt{Lock} interface as well as the \texttt{File}
interface.
An interface may use an interface type name \texttt{T} in place of a
method specification. The effect, called embedding an interface, is
equivalent to enumerating the methods of \texttt{T} explicitly in the
interface.
\begin{Verbatim}[frame=single]
type ReadWrite interface {
Read(b Buffer) bool
Write(b Buffer) bool
}
type File interface {
ReadWrite // same as enumerating the methods in ReadWrite
Lock // same as enumerating the methods in Lock
Close()
}
\end{Verbatim}
An interface type \texttt{T} may not embed itself or any interface type
that embeds \texttt{T}, recursively.
\begin{Verbatim}[frame=single]
// illegal: Bad cannot embed itself
type Bad interface {
Bad
}
// illegal: Bad1 cannot embed itself using Bad2
type Bad1 interface {
Bad2
}
type Bad2 interface {
Bad1
}
\end{Verbatim}
\subsection*{Map types}
A map is an unordered group of elements of one type, called the element
type, indexed by a set of unique \emph{keys} of another type, called the
key type. The value of an uninitialized map is \texttt{nil}.
\begin{Verbatim}[frame=single]
MapType = "map" "[" KeyType "]" ElementType .
KeyType = Type .