当前位置:网站首页>【愚公系列】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函数,中间用英文半角逗号进行分割!
- 可以将一个变量的值,赋值给另外一个变量,并且变量中原有的旧值被新值所覆盖。
边栏推荐
- Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market
- 九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
- AI landing new question type RPA + AI =?
- 全国气象数据/降雨量分布数据/太阳辐射数据/NPP净初级生产力数据/植被覆盖度数据
- 使用Thread类和Runnable接口实现多线程的区别
- How to conduct website testing of software testing? Test strategy let's go!
- 【数模】Matlab allcycles()函数的源代码(2021a之前版本没有)
- A detailed explanation of head pose estimation [collect good articles]
- System framework of PureMVC
- Tiktok may launch an independent grass planting community platform: will it become the second little red book
猜你喜欢

Intel David tuhy: the reason for the success of Intel aoten Technology

窗口可不是什么便宜的东西

DFS和BFS概念及实践+acwing 842 排列数字(dfs) +acwing 844. 走迷宫(bfs)

3GPP信道模型路损基础知识

How to package the parsed Excel data into objects and write this object set into the database?

What about the collapse of win11 playing pubg? Solution to win11 Jedi survival crash

This "advanced" technology design 15 years ago makes CPU shine in AI reasoning

mpf2_线性规划_CAPM_sharpe_Arbitrage Pricin_Inversion Gauss Jordan_Statsmodel_Pulp_pLU_Cholesky_QR_Jacobi

Camera calibration (I): robot hand eye calibration

5G VoNR+之IMS Data Channel概念
随机推荐
What is JVM? What are the purposes of JVM tuning?
计数排序基础思路
JS variable case
R descriptive statistics and hypothesis testing
npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
Depth first traversal template principle of tree and graph
Local tool [Navicat] connects to remote [MySQL] operation
What if win11 pictures cannot be opened? Repair method of win11 unable to open pictures
九章云极DataCanvas公司获评36氪「最受投资人关注的硬核科技企业」
Oracle -- 视图与序列
A series of shortcut keys for jetbrain pychar
JS also exports Excel
STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)
Section 1: (3) logic chip process substrate selection
What about the collapse of win11 playing pubg? Solution to win11 Jedi survival crash
九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
STM32F103实现IAP在线升级应用程序
Flask项目使用flask-socketio异常:TypeError: function() argument 1 must be code, not str
Jetson nano configures pytorch deep learning environment / / to be improved
Lecture 3 of "prime mover x cloud native positive sounding, cost reduction and efficiency enhancement lecture" - kubernetes cluster utilization improvement practice