Go-18-init 2022-09-12 Go Go 0 Comments 见下面的代码输出 1234567891011121314151617var WhatIsThe = AnswerToLife()func AnswerToLife() int { // 1 return 42}func init() { // 2 WhatIsThe = 0}func main() { // 3 if WhatIsThe == 0 { fmt.Println("It's all a lie.") }}//It's all a lie. Golang 中 init 的执行顺序 如果一个包导入了其他包,则首先初始化导入的包。 然后初始化当前包的常量。 接下来初始化当前包的变量。 最后,调用当前包的 init() 函数 参考链接 https://stackoverflow.com/questions/24790175/when-is-the-init-function-run https://learnku.com/go/t/47135