当前位置:网站首页>[Yugong series] go teaching course 005 variables in July 2022
[Yugong series] go teaching course 005 variables in July 2022
2022-07-07 11:23:00 【Hua Weiyun】
One 、 Variable
1. Definition of variables
Variables come from mathematics , Used to describe the data storage space in a computer . Variable can be accessed by variable name . In imperative language , Variables are usually variable ; But in pure functional languages ( Such as Haskell) in , Variables can be immutable (immutable) Of . In some languages , Variables may be explicitly defined to represent variable states 、 An abstraction with storage space ( If in Java and Visual Basic in ); But other languages may use other concepts ( Such as C The object of ) To refer to this abstraction , Without strictly defining “ Variable ” Accurate extension of .
2. Role of variables
The function of variables is to mark and store data in memory .
Memory , Full name memory . It is used to store the data during the operation of the computer .
In order to better store data , The memory is divided into different storage units as follows :
Take a storage unit out of memory , Store the corresponding data :
For example, the above red area , Variable name = The name of the area , Data exists in the region
3. Variable declaration and initialization
3.1 Declaration of variables
Go It's a static language , All variables must be declared before use . The meaning of the declaration is to tell the compiler the boundary information of the memory that the variable can operate on , This boundary is usually provided by the type information of variables . stay Go In language , There is a general variable declaration method like this :
Use of variables
fmt.Println(a)
3.2 Initialization of a variable
You can assign values when defining variables , This process is called variable initialization
var age int=10
3.3 The assignment of a variable
You can assign values to variables after they are defined , Declare before assign .
var age,num int age=10num=20fmt.Println(age,num) //10,20
Assign a variable to another variable as follows :
var age int =10var num intnum=agefmt.Println(num) //10
Be careful : Assigning a value to a variable will overwrite the old value
3.4 Case study : Exchange the values of two variables
Temporary variable
package mainimport "fmt"func main(){ a := 1 b := 5 var t int t = a a = b b = t fmt.Println("a = ", a, "b = ", b )}
Do not use temporary variables
package mainimport "fmt"func main(){ a := 1 b := 5 a = a + b b = a - b a = a - b fmt.Println("a = ", a, "b = ", b )}
Direct assignment
package mainimport "fmt"func main() { a := []int{1, 2} b := []int{3, 4, 5} a, b = b, a fmt.Println(`a:`, a) fmt.Println(`b:`, b)}
summary
- Variable declarations :var Variable name variable type
- Declare multiple variables :var Variable name 1, Variable name … type
- Declare an integer variable , The default value is 0
- Output statements can use only one Println function , The middle part is separated by English half width commas !
- You can set the value of a variable , Assign to another variable , And the old value in the variable is overwritten by the new value .
边栏推荐
- Android interview knowledge points
- Go redis Middleware
- Leetcode - interview question 17.24 maximum submatrix
- Bookmarking - common website navigation for programmers
- Interprocess communication (IPC)
- The eighth training assignment
- Design intelligent weighing system based on Huawei cloud IOT (STM32)
- When initializing 'float', what is the difference between converting to 'float' and adding 'f' as a suffix?
- 使用MeterSphere让你的测试工作持续高效
- Kitex 重试机制
猜你喜欢
关于测试人生的一站式发展建议
RationalDMIS2022阵列工件测量
普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
RationalDMIS2022 高级编程宏程序
[untitled]
Force buckle 1002 Find common characters
Vscode 尝试在目标目录创建文件时发生一个错误:拒绝访问【已解决】
Debezium同步之Debezium架构详解
关于SIoU《SIoU Loss: More Powerful Learning for Bounding Box Regression Zhora Gevorgyan 》的一些看法及代码实现
解决VSCode只能开两个标签页的问题
随机推荐
STM32入门开发 NEC红外线协议解码(超低成本无线传输方案)
JSON format query of MySQL
'module 'object is not callable error
Web端自动化测试失败的原因
QT | multiple windows share a prompt box class
Antd select selector drop-down box follows the scroll bar to scroll through the solution
【C#】WinForm运行缩放(变糊)的解决方法
Go redis Middleware
There are ways to improve self-discipline and self-control
Still cannot find RPC dispatcher table failed to connect in virtual KD
Avoid mutating a prop directly since the value will be overwritten whenever the parent component
数据库同步工具 DBSync 新增对MongoDB、ES的支持
QT implements the delete method of the container
技术分享 | 抓包分析 TCP 协议
Leetcode - interview question 17.24 maximum submatrix
2021-04-08
Array object sorting
博客搬家到知乎
Design intelligent weighing system based on Huawei cloud IOT (STM32)
MPX plug-in