LoginSignup
4
4

More than 5 years have passed since last update.

Go言語の初期化順

Last updated at Posted at 2013-04-15

Goの初期化順を調べたのでメモ
Effective Goに書いてるけど一応確認の意味を込めて

main.go
package main

import "echo"

var _ = echo.Echo("main.var")

func init() {
    echo.Echo("main.init")
}

func main() {
    echo.Echo("main.main")
}
echo.go
package echo

import "fmt"

var _ = Echo("echo.var")

func init() {
    Echo("echo.init")
}

func Echo(s string) string {
    fmt.Println(s)
    return s
}

だと

$ go run main.go
echo.var
echo.init
main.var
main.init
main.main

になるので
まああたり前だけどEffective Goに書いてる通りだった

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4