Go language basic type Analysis _golang

Source: Internet
Author: User

This article analyzes the basic type of go language. Share to everyone for your reference. Specifically as follows:

One, integral type

There are 13 kinds of plastic surgeries in the Go language, 2 of which are just different names, essentially the same, so there are 11 different kinds of reshaping in the go language. As follows:

(1) Int: Depending on the implementation under different platforms, 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 (also known as Byte): (0->255)

(8) Unit16: (0->65535)

(9) Unit32 (also known as Rune): (0->4 294 967 295)

(a) Unit64: (0->18 446 744 073 709 551 615)

(one) Unitptr: Exactly the type that holds the pointer value, the 32-bit platform is unit32, and the 64-bit platform is Unit64

(PS: It is noteworthy that there is no automatic type conversion in the Go language, which means that, in addition to the comparison operator analyzed by the previous blog, the other operators require type conversions for basic operations.) Otherwise, it's a compilation error.

Second, floating point type

There are 2 types of floating-point and two types of plural types in the go language.

(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 in which all functions use float64,

The standard library MATH/CMPLX contains numerous complex-related mathematical functions in which all functions use complex128

(PPS: As in math, the Go language uses suffix I for complex numbers, such as 5 + 5.1i)

Three, string

The go language string is a UTF-8-encoded sequence of variable-width characters, each of which is represented by one or more bytes. This differs from Java in that Java is represented by UTF-16, each character corresponding to 2 bytes.

(1) Create: One is enclosed in double quotes ("), which represents a resolvable string that can be escaped by character. The other is enclosed in single quotes ('), which represent native strings that can contain any character other than inverted quotes, and of course can be wrapped. As follows:

Copy 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 "+ +" operation, can obtain the original byte at the index by [n], through [n:m], [n:], [: M] to obtain the byte corresponding string, if the character is truncated, displays the garbled code. Such as:

Copy Code code as follows:
Func test1 () {
STR1: = "The string of the go language is a sequence of variable-width characters encoded with UTF-8, each character 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))//number of bytes
Fmt. Println ([]rune (STR1))//number of characters
}

The output is:

Words of the word
Words of the word
232
Go language
109
41

(3) The string supports the normal comparison operator operation, which compares the operator in memory by a byte-by-byte comparison string. Of course, this comparison is done in the order of UTF-8 encoding.

(4) The last instance of the operation: Len ([]rune (STR1)), in the Go language, a string can be expressed in a rune (also known as Int32) array, each of which represents a single character. Such as:

Copy Code code as follows:
Func test1 () {
STR1: = "The string of the go language is a sequence of variable-width characters encoded with UTF-8, each character 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

Four, pointers

(1) The nature of the pointer, with a simple example to illustrate:

Copy Code code as follows:
Func test2 () {
A: = "xyz"
B: = "OPQ"
PA: = &a//pa as pointer to a
PP: = &pa//pp as pointer to PA
Fmt. Println (A, B, *pa, **PP)
A + + "zz"//a append "zz"
Fmt. Println (A, B, *pa, **PP)
*pa + + "BB"//pp point to the value, append "BB"
Fmt. Println (A, B, *pa, **PP)
Fmt. Println ("Printing a variety of situations:", &a, a)
Fmt. Println ("Print pa Various situations:", &PA, PA, *pa)
Fmt. Println ("Print pp various situations:", &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 cases: 0xc082038018 0xc0820001d0 XYZZZBB
Print pp Various cases: 0xc082038020 0xc082038018 0xc0820001d0 XYZZZBB

Thus, the nature of the pointer is a variable that holds the logical address.

(2) There are two types of syntax for creating variables, and pointers to them: new (Type) and &type{}, as follows:

Copy 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 you print a pointer to a structure, the go language prints the contents of the struct body after the dereference, and the & is prefixed to indicate that it is a pointer.

I hope this article will help you with your go language program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.