当前第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语言中如何使用模板引擎
为什么docker用Go语言
Go语言 list用法是什么
Go语言函数介绍
Go语言实现string转float的方法
Go语言中一些字符串操作介绍
Go语言有没有优势?
Go语言相关工具怎么安装
Go语言中channel的详细介绍
Go语言的init函数详解
更多相关阅读请进入《Go语言》频道 >>
老貘
一个与时俱进的Go编程知识库。
转载请注明出处:木庄网络博客 » go语言的init函数详解