Fre*_*ski 6 system-calls uptime cross-compiling go sysinfo
我正在尝试检索我的Go应用程序的当前正常运行时间。
我已经看到有一个包syscall提供了type Sysinfo_t一个方法Sysinfo(*Sysinfo_t),它显然允许您检索正常运行时间(因为它是Sysinfo_t结构的一个字段)
到目前为止我所做的是:
sysi := &syscall.Sysinfo_t{}
if err := syscall.Sysinfo(sysi); err != nil {
return http.StatusInternalServerError, nil
}
Run Code Online (Sandbox Code Playgroud)
问题是在编译时我得到了这个:
/path/to/file/res_system.go:43: undefined: syscall.Sysinfo_t
/path/to/file/res_system.go:45: undefined: syscall.Sysinfo
Run Code Online (Sandbox Code Playgroud)
我已经搜索了一点,显然该方法和类型仅在 Linux 上可用,我需要该应用程序在 Linux 和 OsX(我目前正在使用)上运行。
是否有一种交叉兼容的方式来检索应用程序正常运行时间?
注意:我宁愿不使用任何第三方库(除非它们是绝对必要的)
小智 5
获取正常运行时间的简单方法是存储服务启动时间:
https://play.golang.org/p/by_nkvhzqD
package main
import (
"fmt"
"time"
)
var startTime time.Time
func uptime() time.Duration {
return time.Since(startTime)
}
func init() {
startTime = time.Now()
}
func main() {
fmt.Println("started")
time.Sleep(time.Second * 1)
fmt.Printf("uptime %s\n", uptime())
time.Sleep(time.Second * 5)
fmt.Printf("uptime %s\n", uptime())
}
Run Code Online (Sandbox Code Playgroud)