头图
Now we need to understand the data types of GO language.

Boolean

The Boolean value can only be the constant true or false.
A simple example:

package main

import "fmt"

func main() {
    // 声明一个布尔型的变量i
    var i bool = true
    fmt.Println("打印一个布尔型的变量:i=", i)
}

The results of the operation are:
image.png

Number type

Integer type int and floating point type float32, float64
The Go language supports integer and floating-point numbers, and supports complex numbers, where bit operations are complemented.

package main

import "fmt"

func main() {
    // 声明一个数字类型的变量i
    var i int = 100
    fmt.Println("打印一个数字类型的变量:i=", i)
}

The results of the operation are:
image.png

1. Integer type
Serial numbertypedescribe
1uint8Unsigned 8-bit integer (0 to 255)
2uint16Unsigned 16-bit integer (0 to 65535)
3uint32Unsigned 32-bit integer (0 to 4294967295)
4uint64Unsigned 64-bit integer (0 to 18446744073709551615)
5int8Signed 8-bit integer (-128 to 127)
6int16Signed 16-bit integer (-32768 to 32767)
7int32Signed 32-bit integer (-2147483648 to 2147483647)
8int64Signed 64-bit integer (-9223372036854775808 to 9223372036854775807)
2. Floating point
Serial numbertypedescribe
1float32IEEE-754 32-bit floating point number
2float64IEEE-754 64-bit floating point number
3complex6432-bit real and imaginary numbers
4complex12864-bit real and imaginary numbers
3. Other number types
Serial numbertypedescribe
1byteSimilar to uint8
2runeSimilar to int32
3uint32 or 64 bit
4intSame size as uint
5uintptrUnsigned integer, used to store a pointer

String type

A string is a sequence of characters connected by a string of fixed-length characters.
Go's strings are connected by single bytes.
The bytes of Go language strings use UTF-8 encoding to identify Unicode text.

package main

import "fmt"

func main() {
    // 声明一个字符串类型a和一个字符串类型b
    var a = "Hello"
    var b = " World"
    fmt.Println("打印a + b的值为:", a + b)
}

The results of the operation are:
image.png

Zero value

Zero is actually the default value of a variable
If we declare a variable but not initialize it, then Go language will to the zero value of the corresponding type

package main

import "fmt"

func main() {
    // 声明各种类型的数据
    var a int
    var b float64
    var c bool
    var d string
    fmt.Println("打印a的值为:", a)
    fmt.Println("打印b的值为:", b)
    fmt.Println("打印c的值为:", c)
    fmt.Println("打印d的值为:", d)
}

The results of the operation are:
image.png

Derived type

include:

  • (1) Pointer type (Pointer)
  • (2) Array type
  • (3) Structured type (struct)
  • (4) Channel type
  • (5) Function type
  • (6) Slice type
  • (7) Interface type (interface)
  • (8) Map type

Special Instructions

1. It is not necessary to declare the type of the variable here, and let Go deduce it by itself;
var i = 100;
2. You can also declare multiple variables at once
package main

import "fmt"

func main() {
    // 声明一个数字类型的变量a,一个布尔类型的变量b
    var (
        a = 200
        b = false
    )
    fmt.Println("打印a的值为:", a, ";打印b的值为:", b)
}

The results of the operation are:

image.png

3. Short declaration of variables

Go language provides a short declaration of variables: =
Variable name: = expression

package main

import "fmt"

func main() {
    // 简短声明各种类型的数据
    a := 10
    b := 10.01
    c := true
    d := "简短声明"
    fmt.Println("打印a的值为:", a)
    fmt.Println("打印b的值为:", b)
    fmt.Println("打印c的值为:", c)
    fmt.Println("打印d的值为:", d)
}

The results of the operation are:
image.png

4. Constant

Constants are modified with const, and the modified variables cannot be modified

package main

import "fmt"

func main() {
    // 声明一个常量
    const a = 10
    fmt.Print("首次打印a的值为:", a)
    a = 100
    fmt.Println("再次打印a的值为:", a)
}

The results of the operation are:
image.png
An error is reported during operation, and the constant cannot be modified.

5. iota

iota is a constant generator, which can be used to initialize constants with similar rules to avoid repeated initialization

package main

import "fmt"

func main() {
    // 声明几个常量
    const (
        a = iota + 1
        b
        c
        d
    )
    fmt.Print("打印四个值为:", a, b, c, d)
}

The results of the operation are:
image.png

Personal blog address

http://www.zhouzhaodong.xyz/


周兆东
107 声望21 粉丝

一个java小白的成长之路。。。