当前位置:网站首页>【愚公系列】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函数,中间用英文半角逗号进行分割!
- 可以将一个变量的值,赋值给另外一个变量,并且变量中原有的旧值被新值所覆盖。
边栏推荐
- You can't sell the used lithography machine to China! The United States unreasonably pressured the Dutch ASML, and domestic chips were suppressed again
- Introduction to namespace Basics
- sscanf,sscanf_ S and its related usage "suggested collection"
- 软件测试之网站测试如何进行?测试小攻略走起!
- Jetson nano配置pytorch深度学习环境//待完善
- How does vscade use the built-in browser?
- STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)
- 掌握软件安全测试方法秘笈,安全测试报告信手捏来
- acwing 843. n-皇后问题
- What is Web3
猜你喜欢

AI landing new question type RPA + AI =?

Kivy tutorial of setting the size and background of the form (tutorial includes source code)

深入解析Kubebuilder

【736. Lisp 语法解析】

mpf2_ Linear programming_ CAPM_ sharpe_ Arbitrage Pricin_ Inversion Gauss Jordan_ Statsmodel_ Pulp_ pLU_ Cholesky_ QR_ Jacobi

Common methods of list and map

【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑

Common Oracle SQL statements

Windows are not cheap things

Oracle -- 视图与序列
随机推荐
ACL2022 | 分解的元学习小样本命名实体识别
《原动力 x 云原生正发声 降本增效大讲堂》第三讲——Kubernetes 集群利用率提升实践
acwing 843. N-queen problem
If you ask me about R code debugging, I will tell you head, STR, help
R descriptive statistics and hypothesis testing
On the 110th anniversary of Turing's birth, has the prediction of intelligent machine come true?
A line of R code draws the population pyramid
Common Oracle SQL statements
3GPP信道模型路损基础知识
STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
How to package the parsed Excel data into objects and write this object set into the database?
leetcode 53. Maximum Subarray 最大子数组和(中等)
Vscode 如何使用内置浏览器?
Lecture 3 of "prime mover x cloud native positive sounding, cost reduction and efficiency enhancement lecture" - kubernetes cluster utilization improvement practice
What about the collapse of win11 playing pubg? Solution to win11 Jedi survival crash
Vscode automatically adds a semicolon and jumps to the next line
赠票速抢|行业大咖纵论软件的质量与效能 QECon大会来啦
Oracle - views and sequences
SQL where multiple field filtering
JS form get form & get form elements