一个.go文件中可有多个init() 函数吗?

image.png

一个.go文件中可有多个init() 函数吗?他们是否在加载时候,都会执行?有没有先后顺序?

阅读 1.8k
1 个回答
package main

import "fmt"

func main() {
    hello()
}

func hello() {
    fmt.Println("hello")
}
func init() {
    fmt.Println("hello3")
}

func init() {
    fmt.Println("hello2")
}

func init() {
    fmt.Println("hello1")
}

可以按着顺序走

推荐问题