Go主功能未首先操作

Go主功能未首先操作,go,main,process.start,Go,Main,Process.start,我知道主函数是先执行的。 但是,我们确认名为cpu_windows.go的库是首先执行的。 为什么? cpu_windows.go:Times(bool)false cpu_windows.go:common.ProcGetSystemTimes.Call 1 cpu_windows.go:Times(bool)true cpu_windows.go:return perputimes() 1.main.go:main()开始 cpu_windows.go:Times(bool)false cp

我知道主函数是先执行的。 但是,我们确认名为cpu_windows.go的库是首先执行的。 为什么?

cpu_windows.go:Times(bool)false

cpu_windows.go:common.ProcGetSystemTimes.Call 1

cpu_windows.go:Times(bool)true

cpu_windows.go:return perputimes()

1.main.go:main()开始

cpu_windows.go:Times(bool)false

cpu_windows.go:common.ProcGetSystemTimes.Call 1

2.main.go:err

委员会:

如果包具有导入,则在初始化包本身之前会初始化导入的包

程序执行首先初始化主包,然后调用main函数

因此,在
main
中的任何代码之前执行导入的
cpu
包中的

package main
import (
    "bytes"
    "encoding/json"
    "io/ioutil"
    "log"
    "net/http"
    "os"
    "os/signal"
    "strings"
    "unicode/utf8"
    "sync"
    "github.com/robfig/cron"
     cpu "github.com/shirou/gopsutil/cpu"
    "fmt"
)

const NumofResource = 4

// 구조체
type HostInfo struct {
    Hostid string
}

var c *cron.Cron
var lastCPUTimes []cpu.TimesStat

func main() {
    fmt.Println("1.main.go > main() start")
    defer l4g.Close()

var err interface{}
lastCPUTimes, err = cpu.Times(false) //nil
fmt.Println("2.main.go > err", err)
fmt.Println("3.main.go > lastCPUTimes", lastCPUTimes)

if err != nil {
    l4g.Error(err)
}
}