go语言的init函数详解


当前第2页 返回上一页

编译之后执行gprog.exe后的结果表明,gprog.go中的init函数先执行,然后执行了ginit1.go中的两个init函数,然后才执行main函数。

1

2

3

4

5

E:\opensource\go\prj\hellogo>gprog.exe

do in init

do in init1

do in init2

do in main

注:《the way to go》中(P70)有下面红色一句描述,意思是说一个go源文件只能有一个init函数,

但是上面的ginit1.go中的两个init函数编译运行后都正常执行了,

因此这句话应该是笔误。

1

2

3

4

5

6

7

4.4.5 Init-functions

Apart from global declaration with initialization, variables can also be initialized in an init()-function.

This is a special function with the name init() which cannot be called, but is executed automatically

before the main() function in package main or at the start of the import of the package that

contains it.

Every source file can contain only 1 init()-function. Initialization is always single-threaded and

package dependency guarantees correct execution order.

推荐:go视频教程

以上就是go语言的init函数详解的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

国内哪些公司用Go语言

Go语言主要是用来做什么的

Go语言程序如何编译

Go语言适合做哪些开发

go在谷歌:以软件工程为目的的语言设计

Go语言结构体与初始化图文详解

Go语言是脚本语言吗

学习Go语言看什么书

Go语言函数作为值

mac电脑怎么安装Go语言环境

更多相关阅读请进入《Go语言》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...