golang中的init函數(shù)

go語言中init函數(shù)用于包(package)的初始化,該函數(shù)是go語言的一個重要特性,

成都創(chuàng)新互聯(lián)擁有10余年成都網(wǎng)站建設(shè)工作經(jīng)驗,為各大企業(yè)提供成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)服務(wù),對于網(wǎng)頁設(shè)計、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、app開發(fā)定制、wap網(wǎng)站建設(shè)(手機版網(wǎng)站建設(shè))、程序開發(fā)、網(wǎng)站優(yōu)化(SEO優(yōu)化)、微網(wǎng)站、域名申請等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們在互聯(lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了很多網(wǎng)站制作、網(wǎng)站設(shè)計、網(wǎng)絡(luò)營銷經(jīng)驗,集策劃、開發(fā)、設(shè)計、營銷、管理等網(wǎng)站化運作于一體,具備承接各種規(guī)模類型的網(wǎng)站建設(shè)項目的能力。

有下面的特征:

1 init函數(shù)是用于程序執(zhí)行前做包的初始化的函數(shù),比如初始化包里的變量等

2 每個包可以擁有多個init函數(shù)

3 包的每個源文件也可以擁有多個init函數(shù)

4 同一個包中多個init函數(shù)的執(zhí)行順序go語言沒有明確的定義(說明)

5 不同包的init函數(shù)按照包導(dǎo)入的依賴關(guān)系決定該初始化函數(shù)的執(zhí)行順序

6 init函數(shù)不能被其他函數(shù)調(diào)用,而是在main函數(shù)執(zhí)行之前,自動被調(diào)用

下面這個示例摘自《the way to go》,os差異在應(yīng)用程序初始化時被隱藏掉了,

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函數(shù),

2 init函數(shù)是在main函數(shù)之前執(zhí)行的,

3 init函數(shù)被自動調(diào)用,不能在其他函數(shù)中調(diào)用,顯式調(diào)用會報該函數(shù)未定義

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函數(shù)

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

編譯之后執(zhí)行g(shù)prog.exe后的結(jié)果表明,gprog.go中的init函數(shù)先執(zhí)行,然后執(zhí)行了ginit1.go中的兩個init函數(shù),然后才執(zhí)行main函數(shù)。

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函數(shù),

但是上面的ginit1.go中的兩個init函數(shù)編譯運行后都正常執(zhí)行了,

因此這句話應(yīng)該是筆誤。

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語言的init函數(shù)詳解的詳細內(nèi)容,更多請關(guān)注創(chuàng)新互聯(lián)其它相關(guān)文章!

文章標題:golang中的init函數(shù)
文章分享:http://muchs.cn/article10/piesgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、服務(wù)器托管、微信公眾號、標簽優(yōu)化網(wǎng)站改版、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)