为什么Args在函数init之前初始化?

时间:2018-11-16 04:29:21

标签: go

src/os/proc.go

// Args hold the command-line arguments, starting with the program name.
var Args []string

func init() {
    if runtime.GOOS == "windows" {
        // Initialized in exec_windows.go.
        return
    }
    Args = runtime_args()
}

当我在这里调试时,我发现Args在初始化函数之前已初始化。在哪里初始化?

1 个答案:

答案 0 :(得分:1)

如评论所述:// Initialized in exec_windows.go.

src/os/exec_windows.go

func init() {
    p := syscall.GetCommandLine()
    cmd := syscall.UTF16ToString((*[0xffff]uint16)(unsafe.Pointer(p))[:])
    if len(cmd) == 0 {
        arg0, _ := Executable()
        Args = []string{arg0}
    } else {
        Args = commandLineToArgv(cmd)
    }
}