This is a creation in Article, where the information may have evolved or changed.
This article mainly introduces the basic types of Go language, the detailed analysis of the shape, floating point, string, pointer and other types of concrete usage, is to learn more about the go language must grasp the important foundation, the need for friends can refer to the next
This paper analyzes the basic type of go language. Share to everyone for your reference. Specific as follows:
First, the integral type
The go language has 13 kinds of shaping, of which 2 are just different names, the essence is the same, so, in essence go language has 11 kinds of shaping. As follows:
(1) Int: Depending on the implementation of different platforms, it can be int32 or int64
(2) Int8: ( -128->127)
(3) Int16: ( -32768->32767)
(4) Int32: ( -2 147 483 648->2 147 483 647)
(5) Int64:(-9 223 372 036 854 775 808->9 223 372 036 854 775 807)
(6) Unit: Depending on the implementation of different platforms, can be int32 or int64
(7) Unit8 (aka Byte): (0->255)
(8) Unit16: (0->65535)
(9) Unit32 (aka Rune): (0->4 294 967 295)
(Ten) Unit64: (0->18 446 744 073 709 551 615)
(one) Unitptr: The type that fits the pointer value exactly, on the 32-bit platform is unit32, on the 64-bit platform is Unit64
(PS: It is important to note that there is no automatic type conversion in the Go language, which means that, in addition to the comparison operators of the previous post analysis, the other operators basically need type conversions in order to operate.) Otherwise, it is a compilation error)
Second, floating-point type
The go language has 2 types of floating-point and two types of complex types.
(1) float32 ( -3.402...x1038->3.402...x1038)
(2) float64 ( -1.797...x10308->+1.797...x10308)
(3) complex64 (real part, imaginary part is a float32)
(4) complex128 (real part, imaginary part is a float64)
(PS: The standard library math package contains a number of mathematical functions, all of which use float64,
The standard library MATH/CMPLX contains numerous complex-related mathematical functions, all of which use complex128)
(PPS: As in mathematics, the Go language uses the suffix I to denote complex numbers, such as 5 + 5.1i)
Three, string
The go language string is a variable-width sequence of characters encoded with UTF-8, with each character represented by one or more bytes. This differs from Java in that Java is represented by UTF-16, which corresponds to 2 bytes per character.
(1) Create: One is enclosed in double quotation marks ("), which represents a resolvable string that can be escaped by character. The other is enclosed in single quotation marks ('), which represent the original string, which can contain any character except the anti-quote, and can of course be wrapped. As follows:
Copy the code code as follows:
Func test1 () {
STR1: = "\" It ' s me!\ ""
STR2: ="it's me,too"
Fmt. Println (STR1)
Fmt. Println (STR2)
}
The output is:
"It ' s me!"
"It ' s Me,too"
(2) The Go language string supports the "+ =" operation, you can get the original byte at the index by [n], [n:m], [n:], [: M] to get the byte corresponding to the string, if the character is truncated, display garbled. Such as:
Copy the code code as follows:
Func test1 () {
STR1: = "Go language string is a variable width character sequence encoded with UTF-8, each character is represented by one or more bytes." ”
Fmt. Println (Str1[4:15])
Fmt. Println (Str1[5:15])
Fmt. Println (Str1[5])
Fmt. Println (Str1[:5])
Fmt. Println (Len (STR1))//bytes
Fmt. Println (Len ([]rune (STR1))//number of characters
}
The output is:
Word of words
Word of words
232
Go language
109
41
(3) The string supports the general comparison operator operation, comparing the operator in memory with a byte-by-byte comparison string. Of course, this comparison is based on the order of UTF-8 encoding.
(4) The last number of characters in the previous example: Len ([]rune (STR1)), in the Go language, a string can be represented by a Rune (aka Int32) array, each rune represents a single character. Such as:
Copy the code code as follows:
Func test1 () {
STR1: = "Go language string is a variable width character sequence encoded with UTF-8, each character is represented by one or more bytes." ”
For _, Char: = range []rune (str1) {
Fmt. Println (char)
}
}
This operation will print out each character number of the str1 directly
Iv. pointers
(1) The nature of the pointer, with a simple example to illustrate:
Copy the code code as follows:
Func test2 () {
A: = "xyz"
B: = "OPQ"
PA: = &a//pa is a pointer to a
PP: = &pa//pp is a pointer to PA
Fmt. Println (A, B, *pa, **PP)
A + = "zz"//a append "zz"
Fmt. Println (A, B, *pa, **PP)
*pa + = "BB"//pp the value pointed to, append "BB"
Fmt. Println (A, B, *pa, **PP)
Fmt. Println ("Print a various cases:", &a, a)
Fmt. Println ("Print pa Various cases:", &PA, PA, *pa)
Fmt. Println ("Printing pp various conditions:", &PP, pp, *pp, **pp)
}
The output is as follows:
XYZ OPQ XYZ XYZ
Xyzzz OPQ xyzzz xyzzz
XYZZZBB OPQ XYZZZBB XYZZZBB
Print a various cases: 0xc0820001d0 XYZZZBB
Print PA Various conditions: 0xc082038018 0xc0820001d0 XYZZZBB
Print pp Various conditions: 0xc082038020 0xc082038018 0xc0820001d0 XYZZZBB
It can be seen that the essence of a pointer is a variable that holds a logical address.
(2) There are two syntax for creating variables, along with pointers to them: new (Type) and &type{}, as follows:
Copy the code code as follows:
Type point struct {
x int
y int
}
Func test3 () {
A: = Point{1, 2}
B: = new (point)
c: = &point{}
D: = &point{3, 4}
Fmt. Println (A, B, C, D)
}
The output is:
{1 2} &{0 0} &{0 0} &{3 4}
When the go language prints a pointer to a struct, it prints the contents of the structure after the solution is referenced, and the & is prefixed to indicate that it is a pointer.