##uint | 32-bit or 64-bit |
uint8 | Unsigned 8 Bit integer type (0 to 255) |
uint16 | Unsigned 16-bit integer type (0 to 65535) |
uint32 | Unsigned 32-bit integer type (0 to 4294967295) |
uint64 | Unsigned 64-bit integer type (0 to 18446744073709551615) |
int | 32-bit or 64-bit |
int8 | Signed 8-bit integer (-128 to 127) |
int16 | Signed 16-bit integer (-32768 to 32767) |
int32 | Signed 32-bit integer (-2147483648 to 2147483647) |
int64 | Signed 64-bit integer (-9223372036854775808 to 9223372036854775807) |
byte | uint8 alias (type byte = uint8) |
rune | int32 alias (type rune = int32), represents a unicode code |
uintptr | unsigned integer type, used to store a pointer is an unsigned integer type, no specific bit is specified size but large enough to accommodate pointers. The uintptr type is only needed in low-level programming, especially where Go language interacts with C language function library or operating system interface.
|
float32 | IEEE-754 32-bit floating point number |
##float64
IEEE-754 64-bit floating point number |
|
complex64
32-bit real and imaginary number |
|
complex128
64 Real and imaginary numbers |
|
Integer type
Integer data is divided into two categories, signed
and unsigned
two types
Signed: int, int8, int16, int32, int64
Unsigned: uint, uint8, uint16, uint32, uint64, byte
Different bits The difference between the integer types of numbers is that they can store the size of the integer range;
The signed type can store any integer, and the unsigned type can only store natural numbers
The size of int and uint depends on the system , 32-bit system represents int32 and uint32, if it is a 64-bit system, it represents int64 and uint64
byte is similar to uint8, and is generally used to store a single character
To ensure that the program runs correctly, try to Use data types that occupy small space
fmt.Printf("%T", var_name) output variable type
unsafe.Sizeof(var_name) view the bytes occupied by variables
Floating point type
The floating point type is the decimal type and can store decimals. For example, 6.6, -12.34
1. A brief explanation of the storage form of floating point numbers in the machine. Floating point numbers = sign bit, exponent bit, mantissa bit
2. The mantissa part may be lost, resulting in loss of accuracy. . -123.0000901
package main
import "fmt"
func main() {
var num1 float32 = -123.0000901
var num2 float64 = -123.0000901
fmt.Println("num1 = ", num1, "num2 = ", num2);
}
Note: The precision of float64 is more accurate than float32
Note: If we want to save a high-precision number, we should choose float64
3. The storage of floating point type is divided into three parts: sign bit, exponent bit, mantissa bit. During the storage process, the accuracy will be lost
4. The default floating point type of golang is float64 type
5. Under normal circumstances, float64 should be used because it is more accurate than float32
6. 0.123 can be abbreviated as .123, and it also supports scientific notation: 5.1234e2 is equivalent In 512.34
Character
There is no special character type in Golang. If you want to store a single character (letter), byte is generally used to save it.
A string is a sequence of characters connected by a fixed length of characters. Go's strings are connected by single bytes, which means that traditional strings are composed of characters, but Go's strings are different, they are composed of bytes.
- Characters can only be wrapped in single quotes, not double quotes. Double quotes wrap strings
- When we directly output the type value, the ASCII code of the corresponding character is output. Value
- When we want to output the corresponding characters, we need to use formatted output
- Go language characters use UTF-8 encoding, English letters occupy one character, and Chinese characters occupy three characters
- In Go, the essence of a character is an integer. When output directly, it is the UTF-8 encoding code value corresponding to the character.
- You can directly assign a number to a variable, and then when formatting the output %c will output the unicode character corresponding to the number
- The character type can be calculated and is equivalent to an integer , because they all have corresponding unicode codes
package main
import "fmt"
func main() {
//字符只能被单引号包裹,不能用双引号,双引号包裹的是字符串
var c1 byte = 'a'
var c2 byte = '0'
//当我们直接输出type值时,就是输出了对应字符的ASCII码值
//'a' ==> 97
fmt.Println(c1, "--", c2)
//如果我们希望输出对应字符,需要使用格式化输出
fmt.Printf("c2 = %c c2 = %c", c1, c2)
}
But if the characters we save are greater than 255, such as storing Chinese characters, the byte type cannot be saved. You can use uint or int type to save
Discussion on the essence of character type
-
Character type is stored in the computer, and the code value corresponding to the character needs to be stored (Integer) Find out
Storage: Character--> Code value--> Binary--> Storage
Read: Binary-->Code value--> Characters --> Read
The corresponding relationship between characters and code values is determined by the character encoding table (it is specified)
- ## The encoding of #Go language is unified into UTF-8. It is very convenient and unified, and there is no longer the problem of garbled encoding
Boolean typeThe Boolean type is also called the bool type. The bool type data only allows the value true Or falseThe bool type occupies 1 byteThe bool type is suitable for logical operations and is generally used for process controlStringThe string is A sequence of concatenated characters of a fixed length. Go's strings are concatenated from single bytes. The bytes of strings in Go language use UTF-8 encoding to identify Unicode text1. Once a string is assigned a value, it cannot be modified:Strings are immutable in Go.
2. Two identification forms of strings
3. String splicing method" "var str string = "hello " + "world"
str += "!"
4. When one line of string is too long and you need to use multiple lines of string, you can use the following processing //正确写法
str := "hello" +
" world!"
fmt.Println(str)
//错误写法
str := "hello "
+ "world!"
fmt.Println(str)
Pointer
- Basic data type, variables store values, also called value types
- To get the address of a variable, use &, such as var num int, to get the address of num:&num
指针类型,指针变量存的是一个地址,这个地址指向的空间存的才是值,比如:var ptr *int = &num
获取指针类型所指向的值,使用:*,比如,var ptr *int,使用*ptr获取ptr指向的值
指针细节说明:
值类型,都有对应的指针类型,形式为*数据类型,比如int对应的指针就是*int,float64对应的指针类型就是*float64,依此类推。
值类型包括:基本数据类型、数组和结构体struct
值类型与引用类型
值类型和引用类型使用特点:
值类型:变量直接存储值,内存通常在栈中分配
引用类型:变量存储的是一个地址,这个地址对应的空间才真正存储数据(值),内存通常在堆上分配,当没有任何变量应用这个地址时,该地址对应的数据空间就成为一个垃圾,由GC来回收。
Golang中值类型和引用类型的区分
基本数据类型默认值
在Golang中,数据类型都有一个默认值,当程序员没有赋值时,就会保留默认值,在Golang中,默认值也叫做零值。
基本数据类型默认值如下:
数据类型 |
默认值 |
整型 |
0 |
浮点型 |
0 |
字符串 |
"" |
布尔类型 |
false |
package main
import "fmt"
func main() {
var a int
var b float32
var isTrue bool
var str string
//这里的%v,表示按照变量的值输出
fmt.Printf("a = %v, b = %v, isTrue = %v, str = %v", a, b, isTrue, str)
fmt.Println("")
}
推荐学习:Golang教程