When is the init() function run?

前端 未结 11 661
离开以前
离开以前 2020-12-02 03:41

I\'ve tried to find a precise explanation of what the init() function does in Go. I read what Effective Go says but I was unsure if I understood fully what it s

11条回答
  •  自闭症患者
    2020-12-02 04:16

    https://golang.org/ref/mem#tmp_4

    Program initialization runs in a single goroutine, but that goroutine may create other goroutines, which run concurrently.

    If a package p imports package q, the completion of q's init functions happens before the start of any of p's.

    The start of the function main.main happens after all init functions have finished.

提交回复
热议问题