Golang检索应用程序正常运行时间

I'm trying to retrieve the current uptime of my Go application.

I've seen there's a package syscall which provides a type Sysinfo_t and a method Sysinfo(*Sysinfo_t) which apparently allows you to retrieve the Uptime (since it's a field of the Sysinfo_t struct)

What I've done so far is:

sysi := &syscall.Sysinfo_t{}

if err := syscall.Sysinfo(sysi); err != nil {
    return http.StatusInternalServerError, nil
}

The problem is that at compile time I get this:

/path/to/file/res_system.go:43: undefined: syscall.Sysinfo_t
/path/to/file/res_system.go:45: undefined: syscall.Sysinfo

I've searched a bit and apparently that method and type are available only on Linux and I need the application to run both on Linux and OsX (which I'm currently using).

Is there a cross-compatible way to retrieve the application uptime?

NOTE: I'd rather not use any third party libraries (unless they're absolutely necessary)

Simple way to get uptime is to store service start time:

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
", uptime())

    time.Sleep(time.Second * 5)
    fmt.Printf("uptime %s
", uptime())
}

Package syscall was frozen on Go 1.4.

NOTE: This package is locked down. Code outside the standard Go repository should be migrated to use the corresponding package in the golang.org/x/sys repository. That is also where updates required by new systems or versions should be applied. See https://golang.org/s/go1.4-syscall for more information.

Use Sysinfo from golang.org/x/sys it should support this in a cross-platform way, at least on Unix.

You should use Since function from time package.

create time value when application start:

startTime := time.Now()

then ask whenever you want:

uptime := time.Since(startTime)