惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - 心有

go语言最好的帮助在哪里? 我的go语言上机测试代码 解决golang.org不能访问的问题 win7下安装32位GoSublime Package oracle和sybase的帮助文档 用bat文件设置程序启动环境 go语言 windows 32位编译环境搭建 Go没有枚举类型(enums),用const常量的iota替代 oracle数据库性能优化 - 降低IO c语言中的大数运算模块 c#长字符串显示省略号 - 心有 - 博客园 C#的timer类问题~! 计划任务工具 cron 的配置和说明 TRACERT命令及用法 linux下挂载windows的共享文件目录ftp文件夹到/root/wind目录 Linux 用户(user)和用户组(group)管理概述 Linux用户和用户组的管理概述 用NetTerm连接虚拟机的telnet服务,打造轻松自如的虚拟机实验环境 请高人指点下,手机震动时从平台上掉入水中,是用户使用不当还是手机设计缺陷,维修费用由谁承担?
go语言的init函数
心有 · 2013-04-21 · via 博客园 - 心有

go语言中init函数用于包(package)的初始化,该函数是go语言的一个重要特性,

有下面的特征:

1 init函数是用于程序执行前做包的初始化的函数,比如初始化包里的变量等

2 每个包可以拥有多个init函数

3 包的每个源文件也可以拥有多个init函数

4 同一个包中多个init函数的执行顺序go语言没有明确的定义(说明)

5 不同包的init函数按照包导入的依赖关系决定该初始化函数的执行顺序

6 init函数不能被其他函数调用,而是在main函数执行之前,自动被调用

下面这个示例摘自《the way to go》,os差异在应用程序初始化时被隐藏掉了,

var prompt = "Enter a digit, e.g. 3 " + "or %s to quit."

func init() {
    if runtime.GOOS == "windows" {
        prompt = fmt.Sprintf(prompt, "Ctrl+Z, Enter")
    } else { // Unix-like
        prompt = fmt.Sprintf(prompt, "Ctrl+D")
    }
}

下面的两个go文件演示了:

 1 一个package或者是go文件可以包含多个init函数,

 2 init函数是在main函数之前执行的,

 3 init函数被自动调用,不能在其他函数中调用,显式调用会报该函数未定义 

gprog.go代码

package main

import (
    "fmt"
)

// the other init function in this go source file
func init() {
    fmt.Println("do in init")
}

func main() {
    fmt.Println("do in main")
}

func testf() {
    fmt.Println("do in testf")
    //if uncomment the next statment, then go build give error message : .\gprog.go:19: undefined: init
    //init()
}

ginit1.go代码,注意这个源文件中有两个init函数

package main

import (
    "fmt"
)

// the first init function in this go source file
func init() {
    fmt.Println("do in init1")
}

// the second init function in this go source file
func init() {
    fmt.Println("do in init2")
}

编译上面两个文件:go build gprog.go ginit1.go

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

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函数编译运行后都正常执行了,

      因此这句话应该是笔误。

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.

2013.04.21 初稿

2013.04.23 补充说明《the way to go》 中关于init函数的笔误