原文:http://golang.org/doc/go_spec.html
翻译:红猎人 (zengsai@gmail.com)
Boolean types
布尔类型
A boolean type represents the set of Boolean truth values denoted by the predeclared constants true
and false
. The predeclared boolean type is bool
.
布尔类型由预定义的常量 true 和 false 组成,预定义的布尔类型是 bool。
Numeric types
数字类型
A numeric type represents sets of integer or floating-point values. The predeclared architecture-independent numeric types are:
数字类型代表一个数字或浮点值的集合。预定义的类型是:
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 familiar alias for uint8
The value of an n-bit integer is n bits wide and represented using two's complement arithmetic.
There is also a set of predeclared numeric types with implementation-specific sizes:
uint either 32 or 64 bits int either 32 or 64 bits float either 32 or 64 bits complex real and imaginary parts have type float uintptr an unsigned integer large enough to store the uninterpreted bits of a pointer value
To avoid portability issues all numeric types are distinct except byte
, which is an alias for uint8
. Conversions are required when incompatible numeric types are mixed in an expression or assignment. For instance, int32
and int
are not the same type even though they may have the same size on a particular architecture.
String types
A string type represents the set of string values. Strings behave like arrays of bytes but are immutable: once created, it is impossible to change the contents of a string. The predeclared string type is string
.
The elements of strings have type byte
and may be accessed using the usual indexing operations. It is illegal to take the address of such an element; if s[i]
is the ith byte of a string, &s[i]
is invalid. The length of string s
can be discovered using the built-in functionlen
. The length is a compile-time constant if s
is a string literal.
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.
ArrayType = "[" ArrayLength "]" ElementType . ArrayLength = Expression . ElementType = Type .
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 a
can be discovered using the built-in function len(a)
, which is a compile-time constant. The elements can be indexed by integer indices 0 through the len(a)-1
(§Indexes). Array types are always one-dimensional but may be composed to form multi-dimensional types.
[32]byte [2*N] struct { x, y int32 } [1000]*float64 [3][5]int [2][2][2]float64 // same as [2]([2]([2]float64))
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. A slice value may be nil
.
SliceType = "[" "]" ElementType .
Like arrays, slices are indexable and have a length. The length of a slice s
can be discovered by the built-in function len(s)
; unlike with arrays it may change during execution. The elements can be addressed by integer indices 0 through len(s)-1
(§Indexes). 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 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 (§Slices). The capacity of a slice a
can be discovered using the built-in function cap(a)
and the relationship betweenlen()
and cap()
is:
0 <= len(a) <= cap(a)
The value of an uninitialized slice is nil
. The length and capacity of a nil
slice are 0. A new, initialized slice value for a given element type T
is made using the built-in function make
, which takes a slice type and parameters specifying the length and optionally the capacity:
make([]T, length) make([]T, length, capacity)
The make()
call allocates a new, hidden array to which the returned slice value refers. That is, executing
make([]T, length, capacity)
produces the same slice as allocating an array and slicing it, so these two examples result in the same slice:
make([]int, 50, 100) new([100]int)[0:50]
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 make
).
来源:oschina
链接:https://my.oschina.net/u/10896/blog/4148