最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

go時間戳實例(秒、毫秒、納秒)

 更新時間:2026年06月22日 14:48:44   作者:Grassto  
這段文章主要討論了JavaScript與Go語言獲取時間戳的區(qū)別,并強調(diào)了在開發(fā)中應(yīng)進行充分測試的重要性,特別指出Go語言的時間戳單位與JavaScript不同,可能導致數(shù)據(jù)傳輸錯誤

go時間戳(秒、毫秒、納秒)

注意點

js得到的時間戳和go得到的時間戳是不一樣的,js得到的是以毫秒為單位的,而go得到的是以秒或納秒為單位的時間戳。

js

	var timestamp = new Date().getTime()
	console.log(timestamp) //1565084135229 毫秒

go

	now:= time.Now()
	fmt.Println(now.Unix()) // 1565084298 秒
	fmt.Println(now.UnixNano()) // 1565084298178502600 納秒
	fmt.Println(now.UnixNano() / 1e6) // 1565084298178 毫秒

總結(jié)

當初剛接觸go的時候,以為go中的 time.Now().Unix() 得到的時間戳是和js中的 new Date().getTime() 的時間戳是一樣的,導致在數(shù)據(jù)傳輸過程中出現(xiàn)了錯誤,還找了半天的bug,鬧出了笑話。

所以說,在使用自己所沒用過的方法時,不能理所當然,一定要先測試一下,這樣總體會更加的節(jié)省時間。

go1.17(含)版本后,官方提供了直接獲取毫秒,微秒值的方法:

// UnixMilli returns t as a Unix time, the number of milliseconds elapsed since
// January 1, 1970 UTC. The result is undefined if the Unix time in
// milliseconds cannot be represented by an int64 (a date more than 292 million
// years before or after 1970). The result does not depend on the
// location associated with t.
func (t Time) UnixMilli() int64 {
	return t.unixSec()*1e3 + int64(t.nsec())/1e6
}

// UnixMicro returns t as a Unix time, the number of microseconds elapsed since
// January 1, 1970 UTC. The result is undefined if the Unix time in
// microseconds cannot be represented by an int64 (a date before year -290307 or
// after year 294246). The result does not depend on the location associated
// with t.
func (t Time) UnixMicro() int64 {
	return t.unixSec()*1e6 + int64(t.nsec())/1e3
}

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

耿马| 新疆| 新蔡县| 林西县| 丹寨县| 青河县| 芜湖市| 江城| 获嘉县| 北碚区| 江门市| 定陶县| 怀柔区| 青河县| 四子王旗| 贵南县| 伊吾县| 林甸县| 裕民县| 志丹县| 牡丹江市| 高陵县| 汉沽区| 恩平市| 胶州市| 偏关县| 伊春市| 定兴县| 沐川县| 沙湾县| 广水市| 罗定市| 卓尼县| 青海省| 佛冈县| 慈利县| 汝城县| 巴彦淖尔市| 洛川县| 桂林市| 宜兰市|