doulan7166 2017-09-19 05:37
浏览 35

转到主要功能未首先操作

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)
}
}

I know that the main function is executed first. However, we confirmed that the library called cpu_windows.go is executed first. Why?

cpu_windows.go : Times(bool) false

cpu_windows.go : common.ProcGetSystemTimes.Call 1

cpu_windows.go : Times(bool) true

cpu_windows.go : return perCPUTimes()

1.main.go : main() start

cpu_windows.go : Times(bool) false

cpu_windows.go : common.ProcGetSystemTimes.Call 1

2.main.go : err

  • 写回答

1条回答 默认 最新

  • dongyaxiao5884 2017-09-19 05:43
    关注

    The specification says:

    If a package has imports, the imported packages are initialized before initializing the package itself.

    and

    Program execution begins by initializing the main package and then invoking the function main.

    It follows that the initialization code in the imported cpu package is executed before any code in main.

    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算