当前位置:网站首页>Go language slow start - Basic built-in types
Go language slow start - Basic built-in types
2022-07-27 15:39:00 【zy010101】
Basic built-in type
The following table is go Basic types built into the language .
keyword | describe | Take up memory ( byte ) | Type conversion |
|---|---|---|---|
bool | Boolean type ( Only true and false Two values ) | 1 | I won't support it |
int8 | A signed 8 An integer | 1 | Support |
uint8 | Unsigned 8 An integer , Alias byte | 1 | Support |
int16 | A signed 16 An integer | 2 | Support |
uint16 | Unsigned 16 An integer | 2 | Support |
int32 | A signed 32 An integer , Alias rune | 4 | Support |
uint32 | Unsigned 32 An integer | 4 | Support |
int64 | A signed 64 An integer | 8 | Support |
uint64 | Unsigned 64 An integer | 8 | Support |
int | signed int | Rely on the compiler to implement | Support |
uint | Unsigned integer | Rely on the compiler to implement | Support |
uintptr | Unsigned integer , Save address | The compiler must guarantee that uintptr The size of the value of type can store any memory address | Support |
float32 | Single precision floating point | 4 | Support |
float64 | Double precision floating point | 8 | Support |
complex64 | The real part and the virtual part are float32 Type value | 8 | Support |
complex128 | The real part and the virtual part are float64 Type value | 16 | Support |
string | String type | The occupancy size is determined by the string itself | Support |
The table specifically emphasizes whether types support type conversion , This is because go Language is very strict about type , Is a really strongly typed language . A specific example is as follows :
package main
import "fmt"
func main() {
var a = 123 // int type
var b = 1.3 // float64 type
var c = b * float64(a) // Even though a and b They are all numeric types , But because one of them is int type , The other is float64 type ,go Operations between them are not allowed , Therefore, type conversion must be done .
fmt.Println(c)
}This is in C++,Python There is no need for explicit type conversion . And in the go In language , Even if they are all numbers , But its basic types are inconsistent , You must go through explicit type conversion to operate .
type keyword
type Keywords can be used to define types or declare type aliases . Here are some examples :
// Some type definitions declare
type status bool // status and bool It's two different types
type MyString string // MyString and string It's two different types
type Id uint64 // Id and uint64 It's two different types
type real float32 // real and float32 It's two different types
// Some type alias declarations
type boolean = bool // boolean and bool Represents the same type
type Text = string // Text and string Represents the same type
type U8 = uint8 // U8、uint8 and byte Represents the same type
type char = rune // char、rune and int32 Represents the same type
var aa boolean = false
var bb status = false
fmt.Printf("type:%T\n", aa) // Output type:bool
fmt.Printf("type:%T\n", bb) // Output type:main.statusstay go Language buildin.go You can see byte,any,rune Is a type alias , Other types are type definitions .
Zero value
Each type has a zero value . The zero value of a type can be regarded as the default value of this type .
A Boolean zero represents the false of true and false .
Zero values of numeric types are all zero ( But different types of zeros may occupy different space in memory ).
The zero value of a string type is an empty string .Literal constant
- The default type of a string literal is pre declared string type .
- The default type of a Boolean literal is pre declared bool type .
- The default type of an integer literal is pre declared int type .
- One rune The default type of literal is pre declared rune( or int32) type .
- The default type of a floating-point number facet is pre declared float64 type .
- If a literal contains an imaginary literal , The default type of this literal is pre declared complex128 type .
Integer type literal
Integer types support hexadecimal , Decimal system , octal , Binary literal . for example :
fmt.Println(0xABCDEF) // Hexadecimal literal quantity , With 0x perhaps 0X start
fmt.Println(01234567) // Octal literal quantity , With 0 start
fmt.Println(0o1234567) // Octal literal quantity ,go1.13 Then support with 0o perhaps 0O The octal number at the beginning
fmt.Println(0b010101) // Binary literal quantity , With 0b perhaps 0B start Floating point type literal
The literal value of a floating-point number can be as follows :
println(123.123)
println(.1)
println(1.)
println(-0.0)
println(2e2) // Scientific enumeration e Said to 10 Base number ,e The following values are exponents .
println(0x1p2) //go1.13 Hexadecimal floating-point number facets supported later ,p Said to 2 Base number ,p Then there is the index .
println(0x2e2) // because e It's a hexadecimal number , therefore 0x2e2 It's a hexadecimal number , instead of 2x10^2.Plural literal
Because the real part and imaginary part of the complex number type are floating-point numbers , Therefore, the plural type can be as follows :
println(1 + 2i)
println(1.2 + 3.14i)
println(0 + 0i)from Go 1.13 Start , Underline _ Can appear in integers 、 Floating point number and imaginary part number , To be used as a segmenter to enhance readability . for example :
println(100_0000_0000) // Chinese people are used to a group of four numbers
println(9_999_999) // Foreigners are used to a group of three numbers rune Literal value
rune yes int32 Alias for type ,rune Is a special integer type , stay go In language , One rune Value means a Unicode Code points . like C/C++ Medium char type (C/C++ Support ASCII code , One byte is enough , however go Support UTF-8, One byte is not enough to represent Unicode). One rune Literal consists of several characters wrapped in a pair of single quotation marks , The sequence of characters enclosed in single quotation marks represents a Unicode Code point value . for example :
println('a')
println(' countries ')
println('\x61') // 61 yes 97 The hexadecimal representation of
println('\141') // 141 yes 97 Octal representation of
println('\u0061')
println('\U00000061')\ Followed by three octal numeric characters (0-7) It means a byte Value or similar \n,\t Such escape characters , \x It must be followed by two hexadecimal numeric characters (0-9,a-f and A-F) It means a byte value , \u It must be followed by four hexadecimal numeric characters to represent a rune value ( this rune The upper four digits of the value are 0), \U It must be followed by eight hexadecimal numeric characters to represent a rune value . The integer represented by these octal and hexadecimal numeric character sequences must be a legal Unicode Code point value , Otherwise, the compilation will fail . The following is invalid Unicode The code point value will cause go Compile failed .
println('\U99091111') // escape is invalid Unicode code point U+99091111string literal
stay Go in , The string value is UTF-8 Coded .Go There are two literal forms of strings . One is interpretive literal expression (interpreted string literal, Double quotation mark style ). The other is to express it literally (raw string literal, Backquote style ). for example :
print("123\n") // Interpretation form ( Double quotation mark style ) String literal of , Every \n Will be escaped as a newline
print(`123\n`) // Plain form will not \n Escape to newline As mentioned above rune The type is actually go The character type of the language , So naturally, it can be composed of multiple characters . for example :
println("\141\142\143")
println("\x61\x62\x63")
println("\U00001111\U00002222")
println("\uaaaa\ubbbb")For cross platform compatibility , Carriage return in a literal representation in the style of plain inverted quotation marks (Unicode The code point is 0x0D) Will be ignored . A numeric literal can only be used when rounding is not required , Can be used to represent the value of an integer basic type . such as ,1.0 It can represent the value of any basic integer type , but 1.01 But not . When a numeric literal is used to represent a value of a non integer basic type , Round off ( Or the accuracy is lost ) Permissible . for example :
var i int32 = 3.0 // Rounding is not allowed
var f float32 = 0x10000000000000000 // Allowable precision loss
println(i)
println(f) Reference material
https://gfw.go101.org/article/basic-types-and-value-literals.html
边栏推荐
- Network equipment hard core technology insider router Chapter 10 Cisco asr9900 disassembly (III)
- Inside router of network equipment hard core technology (10) disassembly of Cisco asr9900 (4)
- Transactions_ Basic demonstrations and transactions_ Default auto submit & manual submit
- npm install错误 unable to access
- MySQL interview 40 consecutive questions, interviewer, if you continue to ask, I will turn my face
- C language: factorial recursive implementation of numbers
- Leetcode interview question 17.21. water volume double pointer of histogram, monotonic stack /hard
- Network equipment hard core technology insider router Chapter 5 tompkinson roaming the network world (Part 1)
- Some binary bit operations
- Modify spark to support remote access to OSS files
猜你喜欢

Leetcode 240. search two-dimensional matrix II medium

Complexity analysis

【剑指offer】面试题42:连续子数组的最大和——附0x80000000与INT_MIN

【剑指offer】面试题41:数据流中的中位数——大、小堆实现

C语言:字符串函数与内存函数

QT (IV) mixed development using code and UI files

Troubleshooting the slow startup of spark local programs

Summer Challenge harmonyos realizes a hand-painted board

后台返回来的是这种数据,是什么格式啊

【剑指offer】面试题53-Ⅰ:在排序数组中查找数字1 —— 二分查找的三个模版
随机推荐
Network device hard core technology insider router Chapter 15 from deer by device to router (Part 2)
2022-07-27 Daily: IJCAI 2022 outstanding papers were published, and 298 Chinese mainland authors won the first place in two items
Alibaba's latest summary 2022 big factory interview real questions + comprehensive coverage of core knowledge points + detailed answers
js操作dom节点
实现自定义Spark优化规则
go语言慢速入门——基本内置类型
使用解构交换两个变量的值
Two stage submission and three stage submission
C语言:自定义类型
Use double stars instead of math.pow()
Binder初始化过程
Network equipment hard core technology insider router Chapter 21 reconfigurable router
【剑指offer】面试题49:丑数
js运用扩展操作符(…)简化代码,简化数组合并
TCC
Spark 3.0 Adaptive Execution 代码实现及数据倾斜优化
Multi table query_ Sub query overview and multi table query_ Sub query situation 1 & situation 2 & situation 3
【剑指offer】面试题56-Ⅰ:数组中数字出现的次数Ⅰ
Some binary bit operations
shell脚本读取文本中的redis命令批量插入redis