get system boot time in milliseconds in go

To get the system boot time in milliseconds using Go on a Windows system, you can use the GetTickCount64 function from the Windows API through the syscall package. Here's an example:

main.go
package main

import (
    "fmt"
    "strconv"
    "syscall"
    "time"
)

func main() {
    tickCount := syscall.GetTickCount64()
    bootTime := time.Now().Add(-time.Duration(tickCount) * time.Millisecond)
    bootTimeMs := bootTime.UnixNano() / int64(time.Millisecond)
    fmt.Println("System boot time:", strconv.FormatInt(bootTimeMs, 10), "ms ago")
}
356 chars
16 lines

Here, we first call GetTickCount64 to get the system uptime in milliseconds. We then subtract this from the current time using time.Now() and time.Duration(tickCount) * time.Millisecond to get the system boot time. Finally, we convert the boot time to milliseconds, print it out along with a message.

Note that this method only works on a Windows system, and requires you to call the syscall package.

related categories

gistlibby LogSnag