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

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

问题描述

我试图找到对 init() 函数在 Go 中的作用的准确解释.我阅读了 Effective Go 所说的内容,但我不确定我是否完全理解它所说的内容.我不确定的确切句子如下:

<块引用>

finally 的意思是 finally:在包中的所有变量声明都评估了它们的初始化器之后调用 init,并且只有在所有导入的包都被初始化之后才评估它们.

包中的所有变量声明都对它们的初始化器求值是什么意思?这是否意味着如果您在包及其文件中声明全局"变量,init() 将不会运行,直到所有它都被评估,然后它将运行所有 init 函数,然后在运行 ./main_file_name 时运行 main()?

我还阅读了 Mark Summerfield 的围棋书:

<块引用>

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

在我的理解中,init() 仅在您运行打算运行 main() 时才相关,对吗?或主包.任何人都更准确地理解 init() 随时纠正我

解决方案

是的,前提是你有 this:

var WhatIsThe = AnswerToLife()func AnswerToLife() int {//1返回 42}func init() {//2什么是 = 0}func main() {//3如果 WhatIsThe == 0 {fmt.Println(这都是谎言.")}}

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

记住init()总是被调用,不管有没有main,所以如果你导入一个有init函数的包,它会是执行.

此外,每个包可以有多个 init() 函数;它们将按照它们在文件中显示的顺序执行(当然是在所有变量初始化之后).如果它们跨越多个文件,它们将按词法文件名顺序执行(如 @benc 指出的那样):><块引用>

似乎init() 函数是按词法文件名顺序执行的.Go 规范说鼓励构建系统以词法文件名顺序将属于同一包的多个文件呈现给编译器".go build 似乎是这样工作的.


许多内部 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. 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 { // 1
    return 42
}

func init() { // 2
    WhatIsThe = 0
}

func main() { // 3
    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.

Additionally, you can have multiple init() functions per package; they will be executed in the order they show up in the file (after all variables are initialized of course). If they span multiple files, they will be executed in lexical file name order (as pointed out by @benc):

It seems that init() functions are executed in lexical file name order. The Go spec says "build systems are encouraged to present multiple files belonging to the same package in lexical file name order to a compiler". It seems that go build works this way.


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天全站免登陆