当前位置:网站首页>Learn go language 0x03: understand the dependency between variables and initialization order
Learn go language 0x03: understand the dependency between variables and initialization order
2022-04-23 11:07:00 【Love blog uncle】
In this paper, Go Dependencies between variables in language , It's also Go Examples in language specifications , It just expands a little .
* Code
package main
import "fmt"
var (
a = b + c
b = f()
c = f()
d = 3
)
func f() int {
d++
return d
}
func main() {
fmt.Printf("a = %d, b = %d, c = %d, d = %d\n", a, b, c, d)
}
You can rely on intuition , Think about what the output is .
First
thinking
Examination
One
Next
.
.
.
The output is ,a = 9, b = 4, c = 5, d = 5, Did you do it right ?
From this code :
var (
a = b + c
b = f()
c = f()
d = 3
)
func f() int {
d++
return d
}
arely onbandcbrely ondcrely ond
therefore , The initialization order is d,b,c,a.
This dependency can be drawn as a tree , Start from the leaf node of the tree , Until the root of the tree .
When printing , It will be initialized in a way similar to the subsequent traversal of the tree .
for example , Print a when , Need to compute a, Calculation a when , It needs to be calculated first b and c.
Calculation b when , Need to compute d, And then execute d The initialization d=3, here d by 3.
Then you can calculate b 了 , perform f(), here d by 4, therefore ,b by 4 .
And then calculate c , perform f(), here d by 5, therefore ,c by 5 .
And then calculate a,a=4+5, therefore a by 9 .
So the result is :a = 9, b = 4, c = 5, d = 5
* Reference resources
版权声明
本文为[Love blog uncle]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231102106027.html
边栏推荐
- web三大组件(Servlet,Filter,Listener)
- 进程间通信 -- 消息队列
- After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
- MySQL分区表实现按月份归类
- MySQL索引优化之分页探索详细介绍
- 关于JUC三大常用辅助类
- Google Earth Engine(GEE)——将原始影像进行升尺度计算(以海南市为例)
- Go interface usage
- How to Ping Baidu development board
- MySQL面试题讲解之如何设置Hash索引
猜你喜欢

Solutions to common problems in visualization (VIII) solutions to problems in shared drawing area

Visualization Road (11) detailed explanation of Matplotlib color

Solution architect's small bag - 5 types of architecture diagrams

《Neo4j权威指南》简介,求伯君、周鸿袆、胡晓峰、周涛等大咖隆重推荐

About the three commonly used auxiliary classes of JUC

Promise details

Excel · VBA custom function to obtain multiple cell values

Structure of C language (Advanced)

More reliable model art than deep learning

比深度学习更值得信赖的模型ART
随机推荐
How to Ping Baidu development board
mysql中整数数据类型tinyint详解
The difference between restful and soap
Common parameters of ffmpeg command line
Promise details
Structure of C language (Advanced)
Resolution and size of mainstream mobile phones
About the three commonly used auxiliary classes of JUC
妊娠箱和分娩箱的区别
VIM usage
数据库管理软件SQLPro for SQLite for Mac 2022.30
学习 Go 语言 0x01:从官网开始
VIM + ctags + cscope development environment construction guide
MySQL partition table can be classified by month
Oracle connectivity test gadget
精彩回顾|「源」来如此 第六期 - 开源经济与产业投资
Esp32 learning - add folder to project
Google Earth engine (GEE) - scale up the original image (taking Hainan as an example)
Implementation of partition table of existing data table by MySQL
Explain in detail the pitfalls encountered in DTS due to the time zone problems of timestamp and datetime in MySQL