init()函数何时运行? [英] When is the init() function run?

查看:136
本文介绍了init()函数何时运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到Go(Golang)中 init()函数的精确解释。我阅读了 Effective Go 的说法,但我不确定自己是否完全理解其内容。我不确定的确切句子如下:


最后表示finally:init在包中的所有变量声明已经评估之后调用它们的初始值设定项,并且仅在所有导入的包已经初始化后才被评估。

什么是所有包中的变量声明已经评估了它们的初始化符是否意味着?这是否意味着如果你在一个包及其文件中声明了全局变量,init()将不会运行,直到所有的变量都被计算出来,然后运行所有的init函数,然后运行./main_file_name时运行main()。



我还读过马克·萨默菲尔德的书:
$ b


如果一个包有一个或多个init()函数,它们在main包的main()函数被调用之前自动执行。

据我了解, init()只在你运行意图运行main()时才有意义?或主包装。任何人都可以更准确地理解 init()随时纠正我的错误。

是的,假设你有这个

  var WhatIsThe = AnswerToLife()

func AnswerToLife()int {
return 42
}

func init(){
WhatIsThe = 0
}

func main(){
如果WhatIsThe == 0 {
fmt.Println(It's all a lie。)


AnswerToLife()保证在调用 init()之前运行,并且 init()保证在运行之前运行 main()被调用。



请记住 init(),不管是否有main,所以如果你导入一个包含 init 函数的包,它将被执行。



// edit



另外,请记住,您可以有多个 init()函数每个包,它们将按照它们在代码中显示的顺序执行(当然,在所有变量初始化之后)。

//编辑2x



很多内部Go包使用 init()来初始化表等。 ,例如 https://github.com/golang/go/blob /883bc6/src/compress/bzip2/bzip2.go#L480


I've tried to find a precise explanation of what the init() function does in Go (Golang). I read what Effective Go says but I was unsure if I understood fully what it said. The exact sentence I am unsure is the following:

And finally means finally: init is called after all the variable declarations in the package have evaluated their initializers, and those are evaluated only after all the imported packages have been initialized.

What does all the variable declarations in the package have evaluated their initializers mean? Does it mean if you declare "global" variables in a package and its files, init() will not run until all of it is evaluated and then it will run all the init function and then main() when ./main_file_name is ran?

I also read Mark Summerfield's go book the following:

If a package has one or more init() functions they are automatically executed before the main package's main() function is called.

In my understanding, init() is only relevant when you run intend to run main() right? or the Main package. Anyone understands more precisely init() feel free to correct me

解决方案

Yes assuming you have this:

var WhatIsThe = AnswerToLife()

func AnswerToLife() int {
    return 42
}

func init() {
    WhatIsThe = 0
}

func main() {
    if WhatIsThe == 0 {
        fmt.Println("It's all a lie.")
    }
}

AnswerToLife() is guaranteed to run before init() is called, and init() is guaranteed to run before main() is called.

Keep in mind that init() is always called, regardless if there's main or not, so if you import a package that has an init function, it will be executed.

//edit

Also, keep in mind that you can have multiple init() functions per package, they will be executed in the order they show up in the code (after all variables are initialized of course).

//edit 2x

A lot of the internal Go packages use init() to initialize tables and such, for example https://github.com/golang/go/blob/883bc6/src/compress/bzip2/bzip2.go#L480

这篇关于init()函数何时运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆