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:
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:
1. Integer type
Serial number | type | describe |
---|---|---|
1 | uint8 | Unsigned 8-bit integer (0 to 255) |
2 | uint16 | Unsigned 16-bit integer (0 to 65535) |
3 | uint32 | Unsigned 32-bit integer (0 to 4294967295) |
4 | uint64 | Unsigned 64-bit integer (0 to 18446744073709551615) |
5 | int8 | Signed 8-bit integer (-128 to 127) |
6 | int16 | Signed 16-bit integer (-32768 to 32767) |
7 | int32 | Signed 32-bit integer (-2147483648 to 2147483647) |
8 | int64 | Signed 64-bit integer (-9223372036854775808 to 9223372036854775807) |
2. Floating point
Serial number | type | describe |
---|---|---|
1 | float32 | IEEE-754 32-bit floating point number |
2 | float64 | IEEE-754 64-bit floating point number |
3 | complex64 | 32-bit real and imaginary numbers |
4 | complex128 | 64-bit real and imaginary numbers |
3. Other number types
Serial number | type | describe |
---|---|---|
1 | byte | Similar to uint8 |
2 | rune | Similar to int32 |
3 | uint | 32 or 64 bit |
4 | int | Same size as uint |
5 | uintptr | Unsigned 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:
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:
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:
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:
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:
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:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。