如何在 Go 中以納秒為單位獲得單調時間?我需要與以下 C 代碼回傳的值相同的值:
static unsigned long get_nsecs(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000000000UL ts.tv_nsec;
}
包中的函式time似乎回傳當前時間和/或日期。
uj5u.com熱心網友回復:
將 Go 與cgo一起使用。
用于unsigned long long保證納秒的 64 位整數值。例如,在 Windows 上,unsigned long是一個 32 位整數值。
monotonic.go:
package main
import "fmt"
/*
#include <time.h>
static unsigned long long get_nsecs(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (unsigned long long)ts.tv_sec * 1000000000UL ts.tv_nsec;
}
*/
import "C"
func main() {
monotonic := uint64(C.get_nsecs())
fmt.Println(monotonic)
}
$ go run monotonic.go
10675342462493
$
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/439953.html
標籤:走
上一篇:WooCommerce不會觸發自定義訂單狀態的“woocommerce_order_status_changed”掛鉤
