1
1
<!--{
2
2
"Title": "The Go Programming Language Specification",
3
- "Subtitle": "Version of January 31 , 2017",
3
+ "Subtitle": "Version of February 3 , 2017",
4
4
"Path": "/ref/spec"
5
5
}-->
6
6
@@ -685,11 +685,9 @@ <h2 id="Variables">Variables</h2>
685
685
< h2 id ="Types "> Types</ h2 >
686
686
687
687
< p >
688
- A type determines the set of values and operations specific to values of that
689
- type. Types may be < i > named</ i > or < i > unnamed</ i > . Named types are specified
690
- by a (possibly < a href ="#Qualified_identifiers "> qualified</ a > )
691
- < a href ="#Type_declarations "> < i > type name</ i > </ a > ; unnamed types are specified
692
- using a < i > type literal</ i > , which composes a new type from existing types.
688
+ A type determines a set of values together with operations and methods specific
689
+ to those values. A type may be denoted by a < i > type name</ i > , if it has one,
690
+ or specified using a < i > type literal</ i > , which composes a type from existing types.
693
691
</ p >
694
692
695
693
< pre class ="ebnf ">
@@ -702,6 +700,7 @@ <h2 id="Types">Types</h2>
702
700
< p >
703
701
Named instances of the boolean, numeric, and string types are
704
702
< a href ="#Predeclared_identifiers "> predeclared</ a > .
703
+ Other named types are introduced with < a href ="#Type_declarations "> type declarations</ a > .
705
704
< i > Composite types</ i > —array, struct, pointer, function,
706
705
interface, slice, map, and channel types—may be constructed using
707
706
type literals.
@@ -717,16 +716,23 @@ <h2 id="Types">Types</h2>
717
716
</ p >
718
717
719
718
< pre >
720
- type T1 string
721
- type T2 T1
722
- type T3 []T1
723
- type T4 T3
719
+ type (
720
+ A1 = string
721
+ A2 = A1
722
+ )
723
+
724
+ type (
725
+ B1 string
726
+ B2 B1
727
+ B3 []B1
728
+ B4 B3
729
+ )
724
730
</ pre >
725
731
726
732
< p >
727
- The underlying type of < code > string</ code > , < code > T1 </ code > , and < code > T2 </ code >
728
- is < code > string </ code > . The underlying type of < code > []T1 </ code > , < code > T3 </ code > ,
729
- and < code > T4 </ code > is < code > []T1 </ code > .
733
+ The underlying type of < code > string</ code > , < code > A1 </ code > , < code > A2 </ code > , < code > B1 </ code > ,
734
+ and < code > B2 </ code > is < code > string </ code > .
735
+ The underlying type of < code > []B1 </ code > , < code > B3 </ code > , and < code > B4 </ code > is < code > []B1 </ code > .
730
736
</ p >
731
737
732
738
< h3 id ="Method_sets "> Method sets</ h3 >
@@ -1417,11 +1423,10 @@ <h3 id="Type_identity">Type identity</h3>
1417
1423
</ p >
1418
1424
1419
1425
< p >
1420
- Two < a href ="#Types "> named types</ a > are identical if their type names originate in the same
1421
- < a href ="#Type_declarations "> TypeSpec</ a > .
1422
- A named and an < a href ="#Types "> unnamed type</ a > are always different. Two unnamed types are identical
1423
- if the corresponding type literals are identical, that is, if they have the same
1424
- literal structure and corresponding components have identical types. In detail:
1426
+ A < a href ="#Type_definitions "> defined type</ a > is always different from any other type.
1427
+ Otherwise, two types are identical if their < a href ="#Types "> underlying</ a > type literals are
1428
+ structurally equivalent; that is, they have the same literal structure and corresponding
1429
+ components have identical types. In detail:
1425
1430
</ p >
1426
1431
1427
1432
< ul >
@@ -1460,31 +1465,47 @@ <h3 id="Type_identity">Type identity</h3>
1460
1465
1461
1466
< pre >
1462
1467
type (
1463
- T0 []string
1464
- T1 []string
1465
- T2 struct{ a, b int }
1466
- T3 struct{ a, c int }
1467
- T4 func(int , float64) *T0
1468
- T5 func(x int, y float64) *[]string
1468
+ A0 = []string
1469
+ A1 = A0
1470
+ A2 = struct{ a, b int }
1471
+ A3 = int
1472
+ A4 = func(A3 , float64) *A0
1473
+ A5 = func(x int, _ float64) *[]string
1469
1474
)
1475
+
1476
+ type (
1477
+ B0 A0
1478
+ B1 []string
1479
+ B2 struct{ a, b int }
1480
+ B3 struct{ a, c int }
1481
+ B4 func(int, float64) *B0
1482
+ B5 func(x int, y float64) *A1
1483
+ )
1484
+
1485
+ type C0 = B0
1470
1486
</ pre >
1471
1487
1472
1488
< p >
1473
1489
these types are identical:
1474
1490
</ p >
1475
1491
1476
1492
< pre >
1477
- T0 and T0
1493
+ A0, A1, and []string
1494
+ A2 and struct{ a, b int }
1495
+ A3 and int
1496
+ A4, func(int, float64) *[]string, and A5
1497
+
1498
+ B0, B0, and C0
1478
1499
[]int and []int
1479
1500
struct{ a, b *T5 } and struct{ a, b *T5 }
1480
- func(x int, y float64) *[]string and func(int, float64) (result *[]string)
1501
+ func(x int, y float64) *[]string, func(int, float64) (result *[]string), and A5
1481
1502
</ pre >
1482
1503
1483
1504
< p >
1484
- < code > T0 </ code > and < code > T1 </ code > are different because they are named types
1485
- with distinct declarations; < code > func(int, float64) *T0 </ code > and
1486
- < code > func(x int, y float64) *[]string </ code > are different because < code > T0 </ code >
1487
- is different from < code > []string</ code > .
1505
+ < code > B0 </ code > and < code > B1 </ code > are different because they are new types
1506
+ created by distinct < a href =" #Type_definitions " > type definitions </ a > ;
1507
+ < code > func(int, float64) *B0 </ code > and < code > func(x int, y float64) *[]string </ code >
1508
+ are different because < code > B0 </ code > is different from < code > []string</ code > .
1488
1509
</ p >
1489
1510
1490
1511
@@ -1502,7 +1523,7 @@ <h3 id="Assignability">Assignability</h3>
1502
1523
< li >
1503
1524
< code > x</ code > 's type < code > V</ code > and < code > T</ code > have identical
1504
1525
< a href ="#Types "> underlying types</ a > and at least one of < code > V</ code >
1505
- or < code > T</ code > is not a < a href ="#Types " > named type </ a > .
1526
+ or < code > T</ code > is not a < a href ="#Type_definitions " > defined </ a > type .
1506
1527
</ li >
1507
1528
< li >
1508
1529
< code > T</ code > is an interface type and
@@ -1511,7 +1532,7 @@ <h3 id="Assignability">Assignability</h3>
1511
1532
< li >
1512
1533
< code > x</ code > is a bidirectional channel value, < code > T</ code > is a channel type,
1513
1534
< code > x</ code > 's type < code > V</ code > and < code > T</ code > have identical element types,
1514
- and at least one of < code > V</ code > or < code > T</ code > is not a named type.
1535
+ and at least one of < code > V</ code > or < code > T</ code > is not a defined type.
1515
1536
</ li >
1516
1537
< li >
1517
1538
< code > x</ code > is the predeclared identifier < code > nil</ code > and < code > T</ code >
@@ -1840,23 +1861,60 @@ <h3 id="Iota">Iota</h3>
1840
1861
< h3 id ="Type_declarations "> Type declarations</ h3 >
1841
1862
1842
1863
< p >
1843
- A type declaration binds an identifier, the < i > type name</ i > , to a new type
1844
- that has the same < a href ="#Types "> underlying type</ a > as an existing type,
1845
- and operations defined for the existing type are also defined for the new type.
1846
- The new type is < a href ="#Type_identity "> different</ a > from the existing type.
1864
+ A type declaration binds an identifier, the < i > type name</ i > , to a < a href ="#Types "> type</ a > .
1865
+ Type declarations come in two forms: Alias declarations and type definitions.
1866
+ < p >
1867
+
1868
+ < pre class ="ebnf ">
1869
+ TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) .
1870
+ TypeSpec = AliasDecl | TypeDef .
1871
+ </ pre >
1872
+
1873
+ < h4 id ="Alias_declarations "> Alias declarations</ h4 >
1874
+
1875
+ < p >
1876
+ An alias declaration binds an identifier to the given type.
1847
1877
</ p >
1848
1878
1849
1879
< pre class ="ebnf ">
1850
- TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) .
1851
- TypeSpec = identifier Type .
1880
+ AliasDecl = identifier "=" Type .
1852
1881
</ pre >
1853
1882
1883
+ < p >
1884
+ Within the < a href ="#Declarations_and_scope "> scope</ a > of
1885
+ the identifier, it serves as an < i > alias</ i > for the type.
1886
+ </ p >
1887
+
1854
1888
< pre >
1855
- type IntArray [16]int
1889
+ type (
1890
+ nodeList = []*Node // nodeList and []*Node are identical types
1891
+ Polar = polar // Polar and polar denote identical types
1892
+ )
1893
+ </ pre >
1856
1894
1895
+
1896
+ < h4 id ="Type_definitions "> Type definitions</ h4 >
1897
+
1898
+ < p >
1899
+ A type definition binds an identifier to a newly created type
1900
+ with the same < a href ="#Types "> underlying type</ a > and
1901
+ operations as the given type.
1902
+ </ p >
1903
+
1904
+ < pre class ="ebnf ">
1905
+ TypeDef = identifier Type .
1906
+ </ pre >
1907
+
1908
+ < p >
1909
+ The new type is called a < i > defined type</ i > .
1910
+ It is < a href ="#Type_identity "> different</ a > from any other type,
1911
+ including the type it is created from.
1912
+ </ p >
1913
+
1914
+ < pre >
1857
1915
type (
1858
- Point struct{ x, y float64 }
1859
- Polar Point
1916
+ Point struct{ x, y float64 } // Point and struct{ x, y float64 } are different types
1917
+ polar Point // polar and Point denote different types
1860
1918
)
1861
1919
1862
1920
type TreeNode struct {
@@ -1872,8 +1930,9 @@ <h3 id="Type_declarations">Type declarations</h3>
1872
1930
</ pre >
1873
1931
1874
1932
< p >
1875
- The declared type does not inherit any < a href ="#Method_declarations "> methods</ a >
1876
- bound to the existing type, but the < a href ="#Method_sets "> method set</ a >
1933
+ A defined type may have < a href ="#Method_declarations "> methods</ a > associated with it.
1934
+ It does not inherit any methods bound to the given type,
1935
+ but the < a href ="#Method_sets "> method set</ a >
1877
1936
of an interface type or of elements of a composite type remains unchanged:
1878
1937
</ p >
1879
1938
@@ -1901,8 +1960,8 @@ <h3 id="Type_declarations">Type declarations</h3>
1901
1960
</ pre >
1902
1961
1903
1962
< p >
1904
- A type declaration may be used to define a different boolean, numeric, or string
1905
- type and attach methods to it :
1963
+ Type definitions may be used to define different boolean, numeric,
1964
+ or string types and associate methods with them :
1906
1965
</ p >
1907
1966
1908
1967
< pre >
@@ -1924,8 +1983,8 @@ <h3 id="Type_declarations">Type declarations</h3>
1924
1983
< h3 id ="Variable_declarations "> Variable declarations</ h3 >
1925
1984
1926
1985
< p >
1927
- A variable declaration creates one or more variables, binds corresponding
1928
- identifiers to them, and gives each a type and an initial value.
1986
+ A variable declaration creates one or more < a href =" #Variables " > variables</ a > ,
1987
+ binds corresponding identifiers to them, and gives each a type and an initial value.
1929
1988
</ p >
1930
1989
1931
1990
< pre class ="ebnf ">
@@ -2083,8 +2142,8 @@ <h3 id="Method_declarations">Method declarations</h3>
2083
2142
</ p >
2084
2143
2085
2144
< pre class ="ebnf ">
2086
- MethodDecl = "func" Receiver MethodName ( Function | Signature ) .
2087
- Receiver = Parameters .
2145
+ MethodDecl = "func" Receiver MethodName ( Function | Signature ) .
2146
+ Receiver = Parameters .
2088
2147
</ pre >
2089
2148
2090
2149
< p >
@@ -2093,7 +2152,7 @@ <h3 id="Method_declarations">Method declarations</h3>
2093
2152
Its type must be of the form < code > T</ code > or < code > *T</ code > (possibly using
2094
2153
parentheses) where < code > T</ code > is a type name. The type denoted by < code > T</ code > is called
2095
2154
the receiver < i > base type</ i > ; it must not be a pointer or interface type and
2096
- it must be declared in the same package as the method.
2155
+ it must be < a href =" #Type_definitions " > defined </ a > in the same package as the method.
2097
2156
The method is said to be < i > bound</ i > to the base type and the method name
2098
2157
is visible only within < a href ="#Selectors "> selectors</ a > for type < code > T</ code >
2099
2158
or < code > *T</ code > .
0 commit comments