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

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
宝玉的分享
宝玉的分享
V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
MongoDB | Blog
MongoDB | Blog
Hugging Face - Blog
Hugging Face - Blog
H
Help Net Security
博客园 - 三生石上(FineUI控件)
博客园_首页
Jina AI
Jina AI
aimingoo的专栏
aimingoo的专栏
S
SegmentFault 最新的问题
T
Tor Project blog
云风的 BLOG
云风的 BLOG
T
Troy Hunt's Blog
K
Kaspersky official blog
T
Tenable Blog
Project Zero
Project Zero
有赞技术团队
有赞技术团队
S
Schneier on Security
P
Proofpoint News Feed
C
Check Point Blog
GbyAI
GbyAI
P
Proofpoint News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Microsoft Security Blog
Microsoft Security Blog
T
Threatpost
Know Your Adversary
Know Your Adversary
Help Net Security
Help Net Security
D
Docker
U
Unit 42
S
Securelist
月光博客
月光博客
博客园 - Franky
The Hacker News
The Hacker News
L
LangChain Blog
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
Hacker News: Ask HN
Hacker News: Ask HN
S
Security @ Cisco Blogs
AI
AI
人人都是产品经理
人人都是产品经理
The Last Watchdog
The Last Watchdog
Hacker News - Newest:
Hacker News - Newest: "LLM"
I
Intezer
The Register - Security
The Register - Security
阮一峰的网络日志
阮一峰的网络日志

博客园 - 心有

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函数的笔误