当前位置:网站首页>Go 语言变量
Go 语言变量
2022-07-27 22:38:00 【CSPsy】
1. 变量的声明方式
1.1)var 声明
它的声明形式如下:
var name type = expression
- 其中,类型(
type)和表达式(expression)可以省略其中一个,但是不能都省略
例如:
var a int = 1
// 省略 type
var b = "abc"
var c int
fmt.Println(a, b, c)

在省略 expression 时,变量会被赋为零值
- 零值
在 Go 语言中,各个数据类型对应零值如下:bool 值的为 false;int 整数型为 0;float 浮点型为 0.0;string 为 "" 空串;
接口和引用类型(slice、指针、map、通道、函数)为 nil;
复合类型(数组、结构体)为 所有元素的零值;
- 对于多个变量的声明可以放到一个
()内
var (
a int = 1
b = "abc"
c int
)
- 对于多个变量也可以放到一行,省略
type
var a, b, c = 1, "abc", 0
1.2):= 短变量声明
Go 语言还提供了一种短变量声明方式,它的格式如下:
name := expression
- 与
var声明不同的是,短变量声明只能用于函数中
举个例子:
func main() {
// := 声明方式只能用于函数中
a, b, c := 1, "abc", 0
fmt.Println(a, b, c)
}
- Notes
在函数体内,尽量使用短变量声明
当短变量声明,左边的变量数量大于等于 2 时,对于在同一个语法块中已存在的变量,短变量声明的行为等于赋值,短变量声明等于赋值(前提:短变量声明最少声明一个变量)
举个例子:
func main() {
var a int = 1
var b string = "abc"
// 至少要声明一个变量 !!!
a, b, c := 2, "Hello", 0
fmt.Println(a, b, c)
}

1.3)new 函数
Go 语言还可以通过使用内置的 new 函数来创建变量,它的格式如下:
new(T)
表达式 new(T) 创建一个未命名的 T 类型的变量,初始化为对应的零值,返回其地址,所以左边的接收者为一个指针类型的变量。
举个例子:
a:= new(int)
fmt.Println(*a)

它的好处:不需要声明一个虚拟的名字
2. Printf 输出变量的常用转义字符
| 转义字符 | 描述 |
|---|---|
| %d | 十进制整数 |
| %b, %o, %x | 二进制,八进制,十六进制整数 |
| %f, %g, %e | 浮点数 |
| %t | 布尔型:(输出 true / false) |
| %c, %s | 字符,字符串 |
| %q | 带引号的字符(“a”)或字符串(“abc”) |
| %v | 内置格式的任何值 |
| %T | 任何值的类型 |
- Notes
当使用 以 ln 结尾的函数(如 Println) 使用
%v的方式来格式化参数
3. 变量的生命周期
生命周期:在程序执行过程中变量存在的时间段
- 包级别变量
它的生命周期是整个程序的执行时间
- 局部变量
它的生命周期是动态的,变量一直生存到它不可访问(通过垃圾回收机制来回收变量)
垃圾回收机制:
变量的路径源头:
- 每一个包级别的变量
- 每一个当前执行函数的局部变量
追溯该变量的路径源头,通过指针和其他方式的引用可以找到变量,如果变量路径不存在,那么变量就变得不可访问,此时被回收。
边栏推荐
- 网络设备硬核技术内幕 防火墙与安全网关篇 (七) 虚拟化神器 (上)
- Database daily question --- day 22: last login
- Six relationships of UML class diagram, the best way to learn and understand
- I/O设备的基本概念及分类
- Multithreading & high concurrency (the latest in the whole network: interview questions + map + Notes) the interviewer is calm
- 为华为打造无美系设备的产线,台积电三星能做到吗?
- Wavelet transform learning notes
- DEMO:测试接口短时间内接收重复数据创建单据
- How to clearly understand and express IAAs, PAAS and SaaS?
- 推荐系统-指标:ctr、cvr
猜你喜欢

分支和循环语句题目练习

网络安全漏洞分析与漏洞复现

Six relationships of UML class diagram, the best way to learn and understand
![[BuildRelease Management]Parabuild](/img/80/11c2b539c217ecd6ba55668d3e71e9.png)
[BuildRelease Management]Parabuild

Red team killer behinder_ V4.0 (ice scorpion 4.0)

C language programming | explanation and Simulation of offsetof macro
![Jerry Zhi doesn't play hidden audio files [article]](/img/09/b9fb293151f56d2a93f8a1c8f3d0dc.png)
Jerry Zhi doesn't play hidden audio files [article]
![论文赏析[ICLR18]联合句法和词汇学习的神经语言模型](/img/1c/5b9726b16f67dfc2016a0c2035baae.png)
论文赏析[ICLR18]联合句法和词汇学习的神经语言模型

浏览器视频帧操作方法 requestVideoFrameCallback() 简介

How to clearly understand and express IAAs, PAAS and SaaS?
随机推荐
IP address & subnet mask
进程与进程调度
I/O设备的基本概念及分类
范德蒙德卷积 学习笔记
Interface test practical project 02: read interface test documents and practice
自用图床搭建教程
函数相关知识
Jerry, if you turn on Bluetooth again, one for two. When the mobile phone is connected to the prototype, it will appear and cannot be connected [chapter]
融云 IM & RTC 能力上新盘点
深度刨析数据在内存中的存储
MySQL中的运算符
Rancher2.6 monitoring grafana docking LDAP
【原】【爬虫系列】简要获取一下知乎的最热门话题相关主题及描述信息
Recommended system model: DSSM twin tower model for embedded recall
Jerry Zhi has problems in real-time adjustment of sound change effect in magic sound mode [chapter]
7. Typescript part Foundation
Swear, swear, swear
How to clearly understand and express IAAs, PAAS and SaaS?
红队大杀器 Behinder_v4.0(冰蝎4.0)
C语言程序设计 | 单身狗题目讲解