当前第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语言如何实现string转换int
Go语言map(集合)
Go语言如何实现字符串转数组
go 语言结构
为什么用Go语言写区块链
Go语言有没有优势?
Go语言 goto 语句
Go语言如何升级版本
更多相关阅读请进入《Go语言》频道 >>
老貘
一个与时俱进的Go编程知识库。
转载请注明出处:木庄网络博客 » go语言的init函数详解