current position:Home>Categorized input and output, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, go lang basic data types and input and output EP03
Categorized input and output, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, go lang basic data types and input and output EP03
2022-08-06 19:33:10【Liu Yue's technical blog】
携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第6天,点击查看活动详情
前文再续,Go lang和Python一样,There are many categories of basic data types,分门别类,一应俱全.They correspond to different usage scenarios,分别是:整形、浮点、字符、字符串、布尔等等.Commonly used basic data types often participate in the operation of daily business logic、Judgment and input and output operations.
整形 int
Plastic surgery as the name suggests,That is, the data type stored is an integer,Go langIt is divided into signed and unsigned,A simple understanding is the difference in storage range:
有符号整型:int8、int16、int32、int64、int.
无符号整型:uint8、uint16、uint32、uint64、uint.
package main
import (
"fmt"
"math"
"unsafe"
)
// 有符号整型
func Integer() {
var num8 int8 = 127
var num16 int16 = 32767
var num32 int32 = math.MaxInt32
var num64 int64 = math.MaxInt64
var num int = math.MaxInt
fmt.Printf("num8的类型是 %T, num8的大小 %d, num8是 %d\n",
num8, unsafe.Sizeof(num8), num8)
fmt.Printf("num16的类型是 %T, num16的大小 %d, num16是 %d\n",
num16, unsafe.Sizeof(num16), num16)
fmt.Printf("num32的类型是 %T, num32的大小 %d, num32是 %d\n",
num32, unsafe.Sizeof(num32), num32)
fmt.Printf("num64的类型是 %T, num64的大小 %d, num64是 %d\n",
num64, unsafe.Sizeof(num64), num64)
fmt.Printf("num的类型是 %T, num的大小 %d, num是 %d\n",
num, unsafe.Sizeof(num), num)
}
// 无符号整型
func unsignedInteger() {
var num8 uint8 = 128
var num16 uint16 = 32768
var num32 uint32 = math.MaxUint32
var num64 uint64 = math.MaxUint64
var num uint = math.MaxUint
fmt.Printf("num8的类型是 %T, num8的大小 %d, num8是 %d\n",
num8, unsafe.Sizeof(num8), num8)
fmt.Printf("num16的类型是 %T, num16的大小 %d, num16是 %d\n",
num16, unsafe.Sizeof(num16), num16)
fmt.Printf("num32的类型是 %T, num32的大小 %d, num32是 %d\n",
num32, unsafe.Sizeof(num32), num32)
fmt.Printf("num64的类型是 %T, num64的大小 %d, num64是 %d\n",
num64, unsafe.Sizeof(num64), num64)
fmt.Printf("num的类型是 %T, num的大小 %d, num是 %d\n",
num, unsafe.Sizeof(num), num)
}
func main() {
Integer()
println("---------------------------------------")
unsignedInteger()
}
复制代码
程序返回:
num8的类型是 int8, num8的大小 1, num8是 127
num16的类型是 int16, num16的大小 2, num16是 32767
num32的类型是 int32, num32的大小 4, num32是 2147483647
num64的类型是 int64, num64的大小 8, num64是 9223372036854775807
num的类型是 int, num的大小 8, num是 9223372036854775807
---------------------------------------
num8的类型是 uint8, num8的大小 1, num8是 128
num16的类型是 uint16, num16的大小 2, num16是 32768
num32的类型是 uint32, num32的大小 4, num32是 4294967295
num64的类型是 uint64, num64的大小 8, num64是 18446744073709551615
num的类型是 uint, num的大小 8, num是 18446744073709551615
复制代码
这里我们使用fmt.PrintfPrint the corresponding integer data type,和Println不同的是,PrintfVariables can be embedded in print statements using wildcards,But also need to pay attention to type consistency:
%% 一个%字面量
%b 一个二进制整数值(基数为 2),或者是一个(高级的)用科学计数法表示的指数为 2 的浮点数
%c 字符型.可以把输入的数字按照 ASCII 码相应转换为对应的字符
%d 一个十进制数值(基数为 10)
%f 以标准记数法表示的浮点数或者复数值
%o 一个以八进制表示的数字(基数为 8)
%p 以十六进制(基数为 16)表示的一个值的地址,前缀为 0x,字母使用小写的 a-f 表示
%q 使用 Go 语法以及必须时使用转义,以双引号括起来的字符串或者字节切片[]byte,或者是以单引号括起来的数字
%s 字符串.输出字符串中的字符直至字符串中的空字符(字符串以’\0‘结尾,这个’\0’即空字符)
%t 以 true 或者 false 输出的布尔值
%T 使用 Go 语法输出的值的类型
%x 以十六进制表示的整型值(基数为十六),数字 a-f 使用小写表示
%X 以十六进制表示的整型值(基数为十六),数字 A-F 使用小写表示
复制代码
一般情况下,在32Bit system is used32位整形, 64 Under the bit system it is64位整形.
浮点 float
Floating point means that the stored data is a real number,比如 3.1415926.
package main
import (
"fmt"
"math"
)
func showFloat() {
var num1 float32 = math.MaxFloat32
var num2 float64 = math.MaxFloat64
fmt.Printf("num1的类型是%T,num1是%g\n", num1, num1)
fmt.Printf("num2的类型是%T,num1是%g\n", num2, num2)
}
func main() {
showFloat()
}
复制代码
程序返回:
num1的类型是float32,num1是3.4028235e+38
num2的类型是float64,num1是1.7976931348623157e+308
复制代码
We can also do decimal-preserving operations on floating point,Such as percentile display:
package main
import (
"fmt"
//"math"
"reflect"
"strconv"
)
func showFloat() {
numF := 3.1415926
value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", numF), 64)
fmt.Println(reflect.TypeOf(value), value)
}
func main() {
showFloat()
}
复制代码
程序返回:
float64 3.14
复制代码
字符 byte/rune
Each element that makes up a string is called a character:
package main
import (
"fmt"
//"math"
//"reflect"
"unsafe"
)
func showChar() {
var x byte = 65
var y uint8 = 65
fmt.Printf("x = %c\n", x) // x = A
fmt.Printf("y = %c\n", y) // y = A
}
func sizeOfChar() {
var x byte = 65
fmt.Printf("x = %c\n", x)
fmt.Printf("x 占用 %d 个字节\n", unsafe.Sizeof(x))
var y rune = 'A'
fmt.Printf("y = %c\n", y)
fmt.Printf("y 占用 %d 个字节\n", unsafe.Sizeof(y))
}
func main() {
showChar()
sizeOfChar()
}
复制代码
系统输出:
x = A
y = A
x = A
x 占用 1 个字节
y = A
y 占用 4 个字节
复制代码
一望而知,byte 类型只能表示 28个值,So you want to represent some other value,For example in Chinese,就得使用 rune 类型.
字符串 string
Strings are almost one of the most commonly used data types,使用起来也很方便:
package main
import (
"fmt"
)
func main() {
mystring := "hello\tgolang1.18"
fmt.Println(mystring)
}
复制代码
Some strings do not have ready-made literal designations,所以只能用转义字符来表示,比如这里的\t.
布尔 bool
Booleans usually represent true or false,Usually appears in conditional statements:
package main
import (
"fmt"
)
func showBool() {
a := true
b := false
fmt.Println("a=", a)
fmt.Println("b=", b)
fmt.Println("true && false = ", a && b)
fmt.Println("true || false = ", a || b)
}
func main() {
showBool()
}
复制代码
程序返回:
a= true
b= false
true && false = false
true || false = true
复制代码
在Python中,The Boolean data type can participate in numerical operations,Conversion to other types is also possible.但是在 Go 中,truth value is used true 表示,并且 不与 1 相等;同样地,False values are used false 表示,并且不与 0 相等,Relatively strict in use,没有PythonSo sway freely.
数据运算
Common mathematical operators:
+ - * / %(求余) ++ --
复制代码
常见的比较运算符:
== != > < >= <=
复制代码
逻辑运算符:
&& 所谓逻辑与运算符.如果两个操作数都非零,则条件变为真
|| 所谓的逻辑或操作.如果任何两个操作数是非零,则条件变为真
! 所谓逻辑非运算符.使用反转操作数的逻辑状态.如果条件为真,那么逻辑非操后结果为假
复制代码
以及位运算:
& 二进制与操作副本位的结果,如果它存在于两个操作数 (A & B) = 12, 也就是 0000 1100
| 二进制或操作副本,如果它存在一个操作数 (A | B) = 61, 也就是 0011 1101
^ 二进制异或操作副本,如果它被设置在一个操作数就是按位取非 (A ^ B) = 49, 也就是 0011 0001
&^ 二进制位清空&^ (A&^B)=48,也就是110000
<< 二进制左移位运算符.The value of the left operand is shifted to the left by the number of bits specified by the right operand A << 2 =240 也就是 1111 0000
>> Binary right shift operator.The value of the left operand is shifted to the right by the number of bits specified by the right operand A >> 2 = 15 也就是 0000 1111
复制代码
Finally there is the assignment operator:
= 简单的赋值操作符,分配值从右边的操作数左侧的操作数 C = A + B 将分配A + B的值到C
+= 相加并赋值运算符,它增加了右操作数左操作数和分配结果左操作数 C += A 相当于 C = C + A
-= 减和赋值运算符,它减去右操作数从左侧的操作数和分配结果左操作数 C -= A 相当于 C = C - A
*= 乘法和赋值运算符,它乘以右边的操作数与左操作数和分配结果左操作数 C *= A 相当于 C = C * A
/= 除法赋值运算符,它把左操作数与右操作数和分配结果左操作数 C /= A 相当于 C = C / A
%= 模量和赋值运算符,它需要使用两个操作数的模量和分配结果左操作数 C %= A 相当于 C = C % A
<<= 左移位并赋值运算符 C <<= 2 相同于 C = C << 2
>>= 向右移位并赋值运算符 C >>= 2 相同于 C = C >> 2
&= 按位与赋值运算符 C &= 2 相同于 C = C & 2
^= 按位异或并赋值运算符 C ^= 2 相同于 C = C ^ 2
|= Bitwise OR and assignment operator C |= 2 相同于 C = C | 2
复制代码
和Python如出一辙,GolangThe design of data operation is relatively restrained,不像Ruby,Syntactic sugar is like a starry sky,Countless wins.
Input and output of basic data
Go langData input by the user at the terminal can be captured:
package main
import (
"fmt"
)
func main() {
var x int
var y float64
fmt.Println("请输入一个整数,一个浮点类型:")
fmt.Scanln(&x, &y) //读取键盘的输入,通过操作地址,赋值给x和y 阻塞式
fmt.Printf("x的数值:%d,y的数值:%f\n", x, y)
fmt.Scanf("%d,%f", &x, &y)
fmt.Printf("x:%d,y:%f\n", x, y)
}
复制代码
终端运行:
C:\Users\liuyue\www\tornado6>go run test.go
请输入一个整数,一个浮点类型:
1 3.14
x的数值:1,y的数值:3.140000
x:1,y:3.140000
复制代码
藉此,We can complete a small calculator application:
package main
import (
"fmt"
)
func main() {
var x int
var y int
fmt.Println("请输入一个整数")
fmt.Scanln(&x)
fmt.Println("请再输入一个整数")
fmt.Scanln(&y)
sum := x + y
fmt.Printf("两数的和为:%d", sum)
}
复制代码
终端运行:
C:\Users\liuyue\www\tornado6>go run test.go
请输入一个整数
1
请再输入一个整数
2
两数的和为:3
复制代码
Then pass the compile command mentioned earlier,Packaged directly into an application:
C:\Users\liuyue\www\tornado6>go build test.go
C:\Users\liuyue\www\tornado6>test.exe
请输入一个整数
1
请再输入一个整数
2
两数的和为:3
C:\Users\liuyue\www\tornado6>
复制代码
非常方便.
结语
The underlying data type is the smallest unit of code logic,It will be the element,Stored in the following conforming data type,同时,Also participate in branches、循环、judgment and other logic.Knowledge of basic data types,Help strengthen our pairgolangFurther understanding and reflection on basic grammar and design concepts.
copyright notice
author[Liu Yue's technical blog],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/218/202208061926556692.html
The sidebar is recommended
- Vue family bucket - Vue-CLI2 and Vue-CLI3 hands-on teaching
- [Tips] Mac uses commands to view the size of sub-files or sub-directories in a directory
- Why build an index?
- Descartes set type and what is the effect of quantization coding?
- [Written in the romantic moment of Qixi Lang] The solution for obtaining data when encountering http codes 206 and 302 in Go
- [Operating System] Process Creation and Destruction
- AQS synchronization component - CountDownLatch analysis and case
- Why is there an index in quantization coding?
- What is the linear combination type in quantization coding?
- Arduino Painless Development _LED8*8 Dot Matrix Experiment (Detailed)
guess what you like
element ui table changes the default style, removes the border and changes the table background color
Data Structure ----- Quick Sort
Node.js test SMTP service
Create Nginx docker container reverse proxy https
Python batch get gitlab project code
Do phrases created by the second-class dictionary method have to have a specific meaning?
How do I select the quantitative method for the quantitative characteristics of the coding?
What is the result after straight-sum quantization?
What are the types of high-dimensional indexes?
Back-end writing Swagger interface management documentation
Random recommended
- Windows use Telnet to test smtp
- Docker - way to modify folder mapping
- 10 easy-to-use software on mac
- SSL/TLS protocol operating mechanism in https protocol
- What is the certificate chain of trust for HTTPS?Can't you publish it yourself?
- Nginx error 413 Request Entity Too Large solution
- js data manipulation problem solving?
- After changing the scale of the screen and the ratio of the layout, the Vue project feels very slow to change the transparency of the image?
- Hand in hand with you to get started weback4.0 (1)
- How to pass the data obtained by nodejs to the front desk for use (keyword - system file)
- Chapter 24 How much do you know about proxy knowledge in Spring AOP
- The prize pool experience is bad, very dark
- C + + string container
- RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
- The vmware virtual machine is disconnected from the network (nat network) after a period of time
- RuntimeError: module compiled against API version 0xa but this version of numpy is 0x9
- Installing vivado2019.1 is always showing "There is no valid Xilinx installation that this Update can be applied to"
- What are the characteristics of the run-length encoding algorithm?
- Question about pygame
- Self-learning crawler encounters a bottleneck, hoping to get some advice
- Webpack5 packaging process source code analysis (1)
- How to understand the memory analysis of executing another method in a JAVA method.
- HTML5 and CSS web material download
- Chapter 215 Aspect-Oriented Programming Spring AOP Actual Configuration
- ASUS laptop software automatically goes to the recycle bin
- Tag attributes you don't know
- usgs download sentinel2
- andriodstudio packaging process without the steps in the tutorial
- msntfs can not be used!
- Chapter XXVIII Aspect-Oriented Programming of AOP Configuration Based on Spring Annotations
- Question about #vsuninstall#, how to solve it?
- Vue + Element tree form implement drag-and-drop sequence
- Are there any abnormal programs that ZTE R5300G4 server will start at 3:00 am every Saturday?Why does this time cause disk exception
- The error "There is no valid Xilinx installation that this Update can be applied to" has been reported when installing vivado2019.1.
- WeChat Mini Program - Simple Diet Recommendation (3)
- Failed to change color in vscode
- Chapter 217 Play Spring5.X Xml configuration conversion to advanced annotation configuration
- Chapter 216 Play with Spring5.X Xml configuration conversion to annotation configuration
- Linux Network Learning Part VII: Detailed Explanation of IP Protocol + Data Link Layer
- Advanced IO for Linux Learning: Five IO Models