Basic type analysis of GO language

  • 2020-05-07 19:53:26
  • OfStack

This article analyzes the basic types of GO language with examples. Share with you for your reference. The details are as follows:

1. Integral

go language has 13 kinds of reshaping, among which 2 kinds are only different in name but 1 kind in essence, so go language has 11 kinds of reshaping in essence. 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 147483 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, it 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)

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

(11) unitptr: the type that exactly holds the pointer value, unit32 for 32-bit platforms and unit64 for 64-bit platforms

(PS: it is worth noting that there is no automatic type-casting in go, which means that operators other than the comparison operators analyzed in the previous blog post basically require type-casting in order to operate. Otherwise it's a compilation error.)

2. Floating point type

The go language has two types of floating point and two types of complex Numbers.

(1) float32 (3.402... X 1038 - > 3.402... X 1038)

(2) float64 (1.797... X 10308 - > + 1.797... X 10308)

(3) complex64(the real part and the imaginary part are 1 float32)

(4) complex128 (real part and imaginary part are 1 float64)

(PS: the standard library math package contains a number of mathematical functions, all of which use float64.

The standard math/cmplx library contains many mathematical functions related to complex Numbers, all of which use complex128)

(PPS: like 1 in mathematics, Go USES the suffix i for complex Numbers, e.g. 5 + 5.1i)

3. String

The Go language string is a sequence of widened characters encoded in UTF-8, each character represented in one or more bytes. This is different from Java, which is represented by UTF-16 and corresponds to 2 bytes per character.

(1) create: 1 is enclosed in double quotation marks ("), which represents a parsed string and can be escaped by characters. The other is enclosed in single quotes ('), which represent native strings and can contain any character other than the backquotes, as well as line breaks. 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) Go language string support "+=" operation, you can get the original byte in the index by [n], through [n:m], [n:], [m] to get the corresponding byte of the string, if the character is truncated, show the messy code. Such as:

func test1() {
    str1 := "Go Language strings are used UTF-8 Encoded variable-width sequence of characters used for each character 1 Two or more byte representations. "
    fmt.Println(str1[4:15])
    fmt.Println(str1[5:15])
    fmt.Println(str1[5])
    fmt.Println(str1[:5])
    fmt.Println(len(str1))         // The number of bytes
    fmt.Println(len([]rune(str1))) // Number of characters
}

The output is:

The & # 65533; Say the word & # 65533;
Say the word & # 65533;
232
Go language
109
41

(3) strings support regular comparison operator operations, the comparison operator in memory 1 byte 1 byte comparison string. Of course, the comparison is made in the order utf-8 is encoded.

(4) the last character number of the above example: len([]rune(str1)), in Go language, a string can be represented by an array of rune (also known as int32), each rune represents a single character. Such as:

func test1() {
    str1 := "Go Language strings are used UTF-8 Encoded variable-width sequence of characters used for each character 1 Two or more byte representations. "
    for _, char := range []rune(str1) {
        fmt.Println(char)
    }
}

This operation will print out each character number of str1 directly

4. Pointer

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

func test2() {
    a := "xyz"
    b := "opq"
    pa := &a  //pa To point to a A pointer to the
    pp := &pa //pp To point to pa A pointer to the
    fmt.Println(a, b, *pa, **pp)
    a += "zz" //a Additional" zz "
    fmt.Println(a, b, *pa, **pp)
    *pa += "bb" //pp Point to, append "bb"
    fmt.Println(a, b, *pa, **pp)
    fmt.Println(" print a Various 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 for various situations: 0xc0820001d0 xyzzzbb
Print pa for various situations: 0xc082038018 0xc0820001d0 xyzzzbb
Print pp for various situations: 0xc082038020 0xc082038018 0xc0820001d0 xyzzzbb

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

(2) there are two grammars for creating variables and getting Pointers to them at the same time: new(Type) and &Type{}, 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 Go prints a pointer to a structure, it prints the contents of the structure after the dereferenced, prefixed with & to indicate that it is a pointer.

I hope this article has been helpful to your GO language programming.


Related articles: