当前位置:网站首页>[Yugong series] go teaching course 005 variables in July 2022
[Yugong series] go teaching course 005 variables in July 2022
2022-07-07 14:40:00 【Yugong move code】
List of articles
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=10
num=20
fmt.Println(age,num) //10,20
Assign a variable to another variable as follows :
var age int =10
var num int
num=age
fmt.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 main
import "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 main
import "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 main
import "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 .
边栏推荐
- Navigation - are you sure you want to take a look at such an easy-to-use navigation framework?
- 6、Electron无边框窗口和透明窗口 锁定模式 设置窗口图标
- Small game design framework
- 课设之百万数据文档存取
- Ascend 910实现Tensorflow1.15实现LeNet网络的minist手写数字识别
- 小米的芯片自研之路
- 多商户商城系统功能拆解01讲-产品架构
- 什么是云原生?这回终于能搞明白了!
- electron remote 报错
- Leetcode——236. The nearest common ancestor of binary tree
猜你喜欢
Mmkv use and principle
Navigation - are you sure you want to take a look at such an easy-to-use navigation framework?
设备故障预测机床故障提前预警机械设备振动监测机床故障预警CNC震动无线监控设备异常提前预警
华为云数据库DDS产品深度赋能
大厂做开源的五大痛点
The world's first risc-v notebook computer is on pre-sale, which is designed for the meta universe!
Ian Goodfellow, the inventor of Gan, officially joined deepmind as research scientist
Navigation — 这么好用的导航框架你确定不来看看?
Assign a dynamic value to the background color of DataGrid through ivalueconverter
The longest ascending subsequence model acwing 1012 Sister cities
随机推荐
leetcode:648. 单词替换【字典树板子 + 寻找若干前缀中的最短符合前缀】
2022pagc Golden Sail award | rongyun won the "outstanding product technology service provider of the year"
半小时『直播连麦搭建』动手实战,大学生技术岗位简历加分项get!
杭电oj2054 A == B ? ???
[server data recovery] a case of RAID data recovery of a brand StorageWorks server
The longest ascending subsequence model acwing 1014 Mountaineering
Source code analysis of ArrayList
Démontage de la fonction du système multi - Merchant Mall 01 - architecture du produit
Navigation — 这么好用的导航框架你确定不来看看?
2022云顾问技术系列之高可用专场分享会
CVPR2022 | 医学图像分析中基于频率注入的后门攻击
低代码平台中的数据连接方式(下)
The world's first risc-v notebook computer is on pre-sale, which is designed for the meta universe!
Leetcode——236. 二叉树的最近公共祖先
Mmkv use and principle
Nllb-200: meta open source new model, which can translate 200 languages
PAG体验:十分钟完成AE动效部署上线各平台!
Pytorch model trains practical skills and breaks through the bottleneck of speed
多商戶商城系統功能拆解01講-產品架構
【愚公系列】2022年7月 Go教学课程 005-变量