Original article: http://golang.org
Liu Jinyu/Liu yuntao <yuntao. Liu # gmail.com> http://www.log4think.com
Data Type types
Go has some common data types, such as int and float, whose values are expressed by the "applicable" Size of the machine. It also defines data types of specific sizes, such as int8 and float64, and unsigned integer types, such as uint and uint32. These are all completely different data types, even if both int and int32 are 32-bit integers, but they are different types. This is also true for the byte and uint8 types that represent string elements.
When it comes to string, this is also a built-in data type. The value of a string is not only an array of bytes, but its value cannot be changed. Once the value of a string is determined, it cannot be modified. However, the value of a string variable can be changed by re-assigning values. The following code from strings. Go is valid:
11 S: = "hello ";
12 if s [1]! = 'E' {OS. Exit (1 )}
13 s = "good bye ";
14 var p * string = & S;
15 * P = "ciao ";
However, the following code is invalid because it tries to modify the value of a string:
S [0] = 'X ';
(* P) [1] = 'y ';
According to C ++, The go string is a bit similar to the const modifier, And the pointer to the string is similar to the reference of a const string ).
Yes, the ones we see above are pointers. However, pointers in the go language are simplified in usage, which will be mentioned later.
The Declaration of the array is as follows:
VaR arrayofint [10] int;
Arrays are "values" like strings, but they are variable. Different from C, arrayofint in C can be used as a pointer to an int. In go, because the array is a "value", arrayofint is considered (also used) as a pointer to the array.
The size of an array is part of its data type. However, you can declare an slice variable, and then assign values to it with an array pointer pointing to an array with the same element type. More commonly, the form is a [low: high] Slice expression, which indicates the subarray from low to high-1. The Slice type is similar to an array, but it does not explicitly specify the size ([] for [10]), which is used to represent an implicit (usually anonymous) array. If different slice expressions represent data in the same array, they can share the memory of the array, but different arrays will never share memory data.
Slice is more common in Go programs than arrays. It is more flexible and has reference semantics, and is more efficient. The disadvantage is that the storage method cannot be precisely controlled like an array. If you want to save a sequence with 100 elements in a data structure, you should use an array.
When an array parameter is passed to a function, the parameter is declared as slice in most cases. When calling a function, get the array address first, then go will create a slice reference, and then pass this reference.
You can use slice to write this function (from Sum. Go ):
09 func sum (A [] INT) int {// returns an integer
10 s: = 0;
11 For I: = 0; I <Len (a); I ++ {
12 S + = A [I]
13}
14 return s
15}
And then call it like this:
19 s: = sum (& [3] int {1, 2, 3}); // a slice of the array is passed to sum
Note that int is added to the parameter list of sum () to define the return value type (INT ). [3] int {1, 2, 3} is an expression enclosed by braces after a data type. The entire expression constructs a value, here is an array containing three integers. The preceding & indicates the address for extracting this value. This address is implicitly converted into a server Load balancer and sent to sum ().
If you want to create an array, but want the compiler to help you determine the size of the array, you can use... as the array size:
S: = sum (& [...] int {1, 2, 3 });
In actual use, unless you are very concerned about the data structure storage method, slice itself (with [] and without &) is sufficient:
S: = sum ([] int {1, 2, 3 });
In addition, map can be initialized as follows:
M: = map [String] int {"one": 1, "two": 2}
SUM also shows the built-in function Len () for the first time, which is used to return the number of elements. It can be used for strings, arrays, slice, map, and channel.
In addition, the range in the for loop can be used for strings, arrays, slice, MAP, map, and channel. For example
For I: = 0; I <Len (a); I ++ {...}
Traverses each element of a sequence and can be written
For I, V: = range {...}
Among them, I will assign a subscript, V will assign a value to the corresponding value in a, which contains more usage demos.