当前位置:网站首页>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
}
a
rely onb
andc
b
rely ond
c
rely 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
边栏推荐
- Software testers, how to mention bugs?
- 我的创作纪念日
- Visual Road (XII) detailed explanation of collection class
- Visual common drawing (III) area map
- MySQL8. 0 upgraded stepping on the pit Adventure
- Constraintlayout layout
- Microsoft Access database using PHP PDO ODBC sample
- Solutions to common problems in visualization (IX) background color
- MBA-day5数学-应用题-工程问题
- Source insight 4.0 FAQs
猜你喜欢
Mysql8.0安装指南
CUMCM 2021-b: preparation of C4 olefins by ethanol coupling (2)
学习 Go 语言 0x04:《Go 语言之旅》中切片的练习题代码
《Neo4j权威指南》简介,求伯君、周鸿袆、胡晓峰、周涛等大咖隆重推荐
Visual common drawing (V) scatter diagram
Let the LAN group use the remote device
第六站神京门户-------手机号码的转换
Visualization Road (10) detailed explanation of segmentation canvas function
关于JUC三大常用辅助类
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
随机推荐
How to Ping Baidu development board
Data analysis learning (I) data analysis and numpy Foundation
About the three commonly used auxiliary classes of JUC
More reliable model art than deep learning
Google Earth Engine(GEE)——将原始影像进行升尺度计算(以海南市为例)
MySQL对数据表已有表进行分区表的实现
Three web components (servlet, filter, listener)
GO接口使用
MIT:用无监督为世界上每个像素都打上标签!人类:再也不用为1小时视频花800个小时了
Pycharm
学习 Go 语言 0x08:《Go 语言之旅》中 练习使用 error
Visual common drawing (I) stacking diagram
Using El popconfirm and El backtop does not take effect
Ueditor -- limitation of 4m size of image upload component
我的创作纪念日
学习 Go 语言 0x05:《Go 语言之旅》中映射(map)的练习题代码
Database management software sqlpro for SQLite for Mac 2022.30
Implementation of inserting millions of data into MySQL database in 10 seconds
Jupyter lab top ten high productivity plug-ins
Manjaro installation and configuration (vscode, wechat, beautification, input method)