var c = func()string { fmt.Println("variable c initialized.") return"colddish" }()
var b = func()string { fmt.Println("variable b initialized.") return"colddish" }()
var a = func()string { fmt.Println("variable a initialized.") return"colddish" }()
funcShowColddish() { fmt.Println("Colddish") }
运行一下,可以看到打印的顺序如下:
1 2 3 4 5 6 7
variable c initialized. variable b initialized. variable a initialized. package colddish inited. variable m initialized. package menu inited. package main inited.
如果把colddish.go中的变量更改为如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
var c = func()string { fmt.Println("variable c initialized.", b) return"c" }()
var b = func()string { fmt.Println("variable b initialized.", a) return"b" }()
var a = func()string { fmt.Println("variable a initialized.") return"a" }()
此时变量之间存在着依赖,来看看对应的初始化顺序是否会有改变:
1 2 3 4 5 6 7
variable a initialized. variable b initialized. a variable c initialized. b package colddish inited. variable m initialized. package menu inited. package main inited.