当前位置:网站首页>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 .
边栏推荐
- Retinanet网络结构详解
- Jerry caused other messages to accumulate in the message pool [article]
- Recommended system - offline recall: u2tag2i, ICF
- Multithreading and multithreaded programming
- Array related knowledge
- 激活最大化
- [CruiseControl]Build Result JSP
- Matlab 绘制 - 点和向量:向量加减的方法和源码
- Cross desktop web container evolution
- Focus on demand flow rather than idle people
猜你喜欢
随机推荐
Rancher2.6 monitoring grafana docking LDAP
Network device hard core technology insider firewall and security gateway (IX) virtualization artifact (II)
自用图床搭建教程
Maximize activation
Oracle错误: ORA-01722 无效数字
文件系统挂载
【C语言入门】ZZULIOJ 1026-1030
One year anniversary of creation, Chongba young Lang
线性代数 【23】 概念的深入01 - Points坐标点和Vectors向量
IP address & subnet mask
Swoole内存-table详解
【原】【爬虫系列】简要获取一下知乎的最热门话题相关主题及描述信息
Ford SUV "Mustang" officially went offline, safe and comfortable
C language programming | single dog topic explanation
KMP review + AC automata Prequel
When Jerry made a phone call, recording to SD card /u disk was not enough [article]
小波变换学习笔记
Database daily question --- day 22: last login
Network equipment hard core technology insider firewall and security gateway chapter (VI) security double repair under the law
Swoole Task任务使用









