当前位置:网站首页>【愚公系列】2022年7月 Go教学课程 005-变量
【愚公系列】2022年7月 Go教学课程 005-变量
2022-07-06 22:33:00 【InfoQ】
一、变量
1.变量的定义
2.变量的作用



3.变量的声明和初始化
3.1 变量的声明

fmt.Println(a)
3.2 变量的初始化
var age int=10

3.3 变量的赋值
var age,num int
age=10
num=20
fmt.Println(age,num) //10,20
var age int =10
var num int
num=age
fmt.Println(num) //10
3.4 案例:交换两个变量的值
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 )
}
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 )
}
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)
}
总结
- 变量声明:var变量名称变量类型
- 声明多个变量:var变量名称1,变量名称....类型
- 声明整型变量,默认值为0
- 输出语句可以只使用一个Println函数,中间用英文半角逗号进行分割!
- 可以将一个变量的值,赋值给另外一个变量,并且变量中原有的旧值被新值所覆盖。
边栏推荐
- 深入解析Kubebuilder
- Common Oracle SQL statements
- Unit test asp Net MVC 4 Application - unit testing asp Net MVC 4 apps thoroughly
- Deeply cultivate the developer ecosystem, accelerate the innovation and development of AI industry, and Intel brings many partners together
- If you ask me about R code debugging, I will tell you head, STR, help
- 深入解析Kubebuilder
- Meaning of 'n:m' and '1:n' in database design
- 计数排序基础思路
- namespace基础介绍
- Common methods of list and map
猜你喜欢
Depth first traversal template principle of tree and graph
mpf2_ Linear programming_ CAPM_ sharpe_ Arbitrage Pricin_ Inversion Gauss Jordan_ Statsmodel_ Pulp_ pLU_ Cholesky_ QR_ Jacobi
JDBC link Oracle reference code
What if win11 pictures cannot be opened? Repair method of win11 unable to open pictures
AttributeError: module ‘torch._C‘ has no attribute ‘_cuda_setDevice‘
mpf2_线性规划_CAPM_sharpe_Arbitrage Pricin_Inversion Gauss Jordan_Statsmodel_Pulp_pLU_Cholesky_QR_Jacobi
Section 1: (3) logic chip process substrate selection
Deeply cultivate the developer ecosystem, accelerate the innovation and development of AI industry, and Intel brings many partners together
九章云极DataCanvas公司获评36氪「最受投资人关注的硬核科技企业」
DFS和BFS概念及实践+acwing 842 排列数字(dfs) +acwing 844. 走迷宫(bfs)
随机推荐
Advertising attribution: how to measure the value of buying volume?
B站大佬用我的世界搞出卷积神经网络,LeCun转发!爆肝6个月,播放破百万
两个div在同一行,两个div不换行「建议收藏」
What is Web3
DFS and BFS concepts and practices +acwing 842 arranged numbers (DFS) +acwing 844 Maze walking (BFS)
[practice leads to truth] is the introduction of import and require really the same as what is said on the Internet
JDBC link Oracle reference code
Up to 5million per person per year! Choose people instead of projects, focus on basic scientific research, and scientists dominate the "new cornerstone" funded by Tencent to start the application
A line of R code draws the population pyramid
抖音或将推出独立种草社区平台:会不会成为第二个小红书
论文上岸攻略 | 如何快速入门学术论文写作
窗口可不是什么便宜的东西
全国气象数据/降雨量分布数据/太阳辐射数据/NPP净初级生产力数据/植被覆盖度数据
Data security -- 12 -- Analysis of privacy protection
What about the collapse of win11 playing pubg? Solution to win11 Jedi survival crash
组织实战攻防演练的5个阶段
R语言主成分pca、因子分析、聚类对地区经济研究分析重庆市经济指标
什么是Web3
Run the command once per second in Bash- Run command every second in Bash?
What is JVM? What are the purposes of JVM tuning?