当前位置:网站首页>Go的变量
Go的变量
2022-06-12 16:43:00 【二进制杯莫停】
1 变量
1.1 变量的声明
package main //必须有一个main包
import "fmt" //导入包,必须使用
func main() {
//1. 声明格式 var 变量名 类型;声明变量了,必须使用
//2. 只是声明没有初始化的变量,默认值是0
//3. 同一个{}里,声明的变量名是唯一的
var a int
fmt.Println("a = ", a)
//4. 可以同时声明多个变量
//var b,c int
a = 10 // 变量的赋值
fmt.Println("a = ", a)
}
控制台输出:
$ go run xxx.go
a = 0
a = 10
1.2 变量初始化和自动推导类型
package main //必须有一个main包
import "fmt" //导入包,必须使用
func main() {
//2. 变量的初始化,声明变量时,同时初始化
var b int = 10 //初始化,并赋值
b = 20 //赋值,先声明,后赋值
fmt.Println("b = ", b)
//3. 自动推导类型,必须初始化,通过初始化类型推导变量类型
c := 30
//%T打印变量所属类型
fmt.Printf("c type is %T\n", c)
}
控制台输出:
$ go run xxx.go
b = 20
c type is int
1.3 多重赋值和匿名变量
package main //必须有一个main包
import "fmt" //导入包,必须使用
func main() {
//4. 多重赋值
i, j := 10, 20
fmt.Printf("i = %d, j = %d\n", i, j)
//5. 交换两个变量的值
i, j = j, i
fmt.Printf("i = %d, j = %d\n", i, j)
//_:匿名变量 丢弃数据不处理,_匿名变量配合函数返回值使用才有优势
_, res, _ := test()
fmt.Printf("res = %d\n", res)
}
func test()(a,b,c int){
return 1, 2, 3
}
控制台输出:
$ go run xxx.go
i = 10, j = 20
i = 20, j = 10
res = 2
1.4 多个变量或变量的定义
package main
import "fmt"
func main(){
//不同类型变量的声明(定义)
//var a int = 10
//var b float64 = 3.14
var(
a int
b float64
)
a, b = 10, 3.14
fmt.Println("a = ", a)
fmt.Println("b = ", b)
const(
i int = 4
j float64 = 3.14
)
fmt.Println("i = ", i)
fmt.Println("j = ", j)
}
控制台输出:
$ go run xxx.go
a = 10
b = 3.14
i = 4
j = 3.14
2 常量
2.1 常量定义
package main //必须有一个main包
import "fmt" //导入包,必须使用
func main() {
//变量:程序运行期间,可以改变的量,变量声明需要var
//常量:程序运行期间,不可以改变的量,常量声明需要const
const p int = 10
fmt.Println("p = ", p)
const q = 20
fmt.Println("q = ", q)
fmt.Printf("q type is %T\n", q)
}
控制台输出:
p = 10
q = 20
q type is int
2.2 常量自动生成器 - iota
package main
import "fmt"
func main(){
//1. iota常量自动生成器,每隔一行,移动累加1
//2. iota给常量赋值使用
const(
a = iota
b = iota
c = iota
)
fmt.Printf("a = %d, b = %d, c = %d\n", a, b, c)
//3. iota遇到const,重置为0
const d = iota
fmt.Printf("d = %d\n", d)
//4. 可以只写一个iota
const(
a1 = iota
b1
c1
)
fmt.Printf("a1 = %d, b1 = %d, c1 = %d\n", a1, b1, c1)
const a2 = iota
const b2 = iota
const c2 = iota
fmt.Printf("a2 = %d, b2 = %d, c2 = %d\n", a2, b2, c2)
//5. 如果是同一行,值都一样
const(
i = iota
j1, j2, j3 = iota, iota, iota
k = iota
)
fmt.Printf("i = %d, j1 = %d, j2 = %d, j3 = %d, k = %d\n", i, j1, j2, j3, k)
}
控制台输出:
$ go run xxx.go
a = 0, b = 1, c = 2
d = 0
a1 = 0, b1 = 1, c1 = 2
a2 = 0, b2 = 0, c2 = 0
i = 0, j1 = 1, j2 = 1, j3 = 1, k = 2
边栏推荐
- [adult Liu Er - pytorch deep learning practice] notes with learning (I)
- latex表格 在线生成
- Unit sshd. service could not be found
- Uniapp壁纸小程序源码/双端微信抖音小程序源码
- Project training of Shandong University rendering engine system (VII)
- [DSP video tutorial] DSP video tutorial Issue 8: performance comparison of DSP library trigonometric function, C library trigonometric function and hardware trigonometric function, and accuracy compar
- 软件工程 学生信息管理系统 结构化的需求分析
- QCustomplot笔记(一)之QCustomplot添加数据以及曲线
- How to do a good job of testing in the company (do a good job of testing)
- 并发包和AQS
猜你喜欢

男神女神投票源码 v5.5.21 投票源码
![[Hunan University] information sharing of the first and second postgraduate entrance examinations](/img/15/298ea6f7367741e1e085007c498e51.jpg)
[Hunan University] information sharing of the first and second postgraduate entrance examinations

Structural requirement analysis of software engineering student information management system

Qt开发高级进阶:初探qt + opengl

Recommend AI intelligent drawing repair software

有哪些特容易考上的院校?

Leetcode 2190. The number that appears most frequently in the array immediately after the key (yes, once)

Mongodb learning and sorting (basic command learning of users, databases, collections and documents)

'virtue and art' in the field of recurrent+transformer video recovery

武汉大学甘菲课题组和南昌大学徐振江课题组联合招聘启事
随机推荐
[raspberry pie]: (IV) camera advanced
The C programming language (2nd Edition) notes / 7 input and output / 7.8 other functions
[DSP video tutorial] DSP video tutorial Issue 8: performance comparison of DSP library trigonometric function, C library trigonometric function and hardware trigonometric function, and accuracy compar
What's the matter with pbootcms' if judgment failure and direct display of labels?
Object. Keys traverses an object
'virtue and art' in the field of recurrent+transformer video recovery
\begin{algorithm} 笔记
About component value transfer
Mongodb learning and sorting (basic command learning of users, databases, collections and documents)
js監聽用戶是否打開屏幕焦點
Iscc-2022 part WP
pytorch和torchvision官方文档使用方法
Analysis of Nacos config dynamic refresh source code
goland变成中文版了怎么修改回英文版
收藏 | 22个短视频学习Adobe Illustrator论文图形编辑和排版
Possible problems of long jump in gaussdb
JS monitors whether the user opens the screen focus
Collect | 22 short videos to learn Adobe Illustrator paper graphic editing and typesetting
ISCC-2022 部分wp
Leetcode 2194. Excel 表中某个范围内的单元格(可以,已解决)