当前位置:网站首页>Go language variable
Go language variable
2022-07-28 01:11:00 【CSPsy】
Go Language variables
1. How variables are declared
1.1)var Statement
Its declaration form is as follows :
var name type = expression
- among , type (
type) And expressions (expression) One of them can be omitted , But we can't omit all of them
for example :
var a int = 1
// Omit type
var b = "abc"
var c int
fmt.Println(a, b, c)

In omitting expression when , The variable will be assigned Zero value
- Zero value
stay Go In language , The corresponding zero values of each data type are as follows :bool The value is false;int Integer type is 0;float Floating point is 0.0;string by "" Empty string ;
Interface and reference types (slice、 The pointer 、map、 passageway 、 function ) by nil;
The compound type ( Array 、 Structure ) by Zero value of all elements ;
- The declaration of multiple variables can be put into one
()Inside
var (
a int = 1
b = "abc"
c int
)
- Multiple variables can also be placed on one line , Omit
type
var a, b, c = 1, "abc", 0
1.2):= Short variable declaration
Go Language also provides a way to declare short variables , The format is as follows :
name := expression
- And
varThe difference is that , Short variable declarations can only be used for functions in
for instance :
func main() {
// := Declaration can only be used in functions
a, b, c := 1, "abc", 0
fmt.Println(a, b, c)
}
- Notes
stay Function body , As far as possible Declare with short variables
When short variables are declared , The variable on the left The quantity is greater than or equal to 2 when , For in The same grammar block in Existing variables , The behavior of short variable declarations Equal to assignment , Short variable declaration equals assignment ( Premise : Short variable declarations declare at least one variable )
for instance :
func main() {
var a int = 1
var b string = "abc"
// At least one variable must be declared !!!
a, b, c := 2, "Hello", 0
fmt.Println(a, b, c)
}

1.3)new function
Go The language can also be used by using the built-in new Function to create a variable , The format is as follows :
new(T)
expression new(T) Create an unnamed T Variable of type , Initialize to the corresponding zero value , Return its address , So the receiver on the left is a pointer variable .
for instance :
a:= new(int)
fmt.Println(*a)

Its benefits : There is no need to declare a virtual name
2. Printf Common escape characters of output variables
| Escape character | describe |
|---|---|
| %d | Decimal integer |
| %b, %o, %x | Binary system , octal , Hexadecimal integer |
| %f, %g, %e | Floating point numbers |
| %t | Boolean type :( Output true / false) |
| %c, %s | character , character string |
| %q | Quoted characters (“a”) Or a string (“abc”) |
| %v | Any value in built-in format |
| %T | The type of any value |
- Notes
When using With ln At the end of the function ( Such as Println) Use
%vThe way To format parameters
3. Life cycle of variable
Life cycle : The period of time during which variables exist during program execution
- Package level variables
Its life cycle is The execution time of the whole program
- local variable
Its life cycle is Dynamic , Variable always Survive until it is inaccessible ( Collect variables through garbage collection mechanism )
Garbage collection mechanism :
Path source of variables :
- Variables at each package level
- Local variables of each currently executing function
Trace the path source of this variable , adopt Pointers and other references Variables can be found , If the variable path does not exist , Then the variable becomes inaccessible , At this time, it is recycled .
边栏推荐
- Understanding of cap
- C language programming | single dog topic explanation
- Retinanet网络结构详解
- [proteus simulation] 51 single chip microcomputer washing machine simulation control program
- 代码随想录笔记_哈希_1002查找共用字符
- 一周年创作纪念日,冲吧少年郎
- How to smoothly go online after MySQL table splitting?
- Network device hard core technology insider firewall and security gateway (10)
- 文件系统挂载
- 学习笔记12:Eratosthenes筛选法求素数(100以内) 和 魔方阵
猜你喜欢

【OpenCV 例程 300篇】241. 尺度不变特征变换(SIFT)

iperf安装与使用

UML类图的六大关系,最佳学习理解方式

代码随想录笔记_哈希_1002查找共用字符

Iperf installation and use

Ink wheel salon | Li Wenjie, Peking University: a graph database system for knowledge atlas application gstore

【STM32】看门狗模块

Ford SUV "Mustang" officially went offline, safe and comfortable

C language programming | single dog topic explanation

分支和循环语句题目练习
随机推荐
Recommended system - offline recall: u2tag2i, ICF
为华为打造无美系设备的产线,台积电三星能做到吗?
[BuildRelease Management]Parabuild
110. In depth introduction to sap ui5 fileuploader control - why do you need a hidden iframe
Vandermond convolution learning notes
0-1背包问题
红队大杀器 Behinder_v4.0(冰蝎4.0)
Use of postman
uniapp显示富文本效果demo(整理)
IP address & subnet mask
canvas模拟输入框输入
程序员成长第三十篇:你真的懂反馈吗?
Focus on demand flow rather than idle people
《KMP复习 + AC自动机》前传
Network device hard core technology insider firewall and security gateway (VII) virtualization artifact (Part 1)
At least 42 employees are infected with novel coronavirus! Nokia announces closure of telecom equipment plant in India
Swear, swear, swear
Redis缓存穿透击穿和雪崩
Analysis and recurrence of network security vulnerabilities
UML类图的六大关系,最佳学习理解方式