当前位置:网站首页>【愚公系列】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函数,中间用英文半角逗号进行分割!
- 可以将一个变量的值,赋值给另外一个变量,并且变量中原有的旧值被新值所覆盖。
边栏推荐
- 赠票速抢|行业大咖纵论软件的质量与效能 QECon大会来啦
- Factor analysis r practice (with R installation tutorial and code)
- What is Web3
- Meow, come, come: do you really know if, if else
- Advertising attribution: how to measure the value of buying volume?
- On the 110th anniversary of Turing's birth, has the prediction of intelligent machine come true?
- Depth first traversal template principle of tree and graph
- AI landing new question type RPA + AI =?
- 深入解析Kubebuilder
- 软件测试之网站测试如何进行?测试小攻略走起!
猜你喜欢
Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
Intel David tuhy: the reason for the success of Intel aoten Technology
AI landing new question type RPA + AI =?
Oracle -- 视图与序列
Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot microservice code analysis and dialogue experim
This "advanced" technology design 15 years ago makes CPU shine in AI reasoning
How does vscade use the built-in browser?
Meow, come, come: do you really know if, if else
5G VoNR+之IMS Data Channel概念
The request request is encapsulated in uni app, which is easy to understand
随机推荐
Master the secrets of software security testing methods, and pinch the security test report with your hands
论文上岸攻略 | 如何快速入门学术论文写作
The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
Field data acquisition and edge calculation scheme of CNC machine tools
If you ask me about R code debugging, I will tell you head, STR, help
Case reward: Intel brings many partners to promote the innovation and development of multi domain AI industry
軟件測試之網站測試如何進行?測試小攻略走起!
每人每年最高500万经费!选人不选项目,专注基础科研,科学家主导腾讯出资的「新基石」启动申报
Thesis landing strategy | how to get started quickly in academic thesis writing
AttributeError: module ‘torch._C‘ has no attribute ‘_cuda_setDevice‘
NTU notes 6422quiz review (1-3 sections)
[digital analog] source code of MATLAB allcycles() function (not available before 2021a)
树与图的深度优先遍历模版原理
计数排序基础思路
九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
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
How does vscade use the built-in browser?
Vscode 如何使用内置浏览器?
3GPP信道模型路损基础知识
namespace基础介绍