聊聊gost的CountWatch

本文主要研究一下gost的CountWatch

CountWatch

gost/time/count.go

type CountWatch struct {
	start time.Time
}

func (w *CountWatch) Start() {
	var t time.Time
	if t.Equal(w.start) {
		w.start = time.Now()
	}
}

func (w *CountWatch) Reset() {
	w.start = time.Now()
}

func (w *CountWatch) Count() int64 {
	return time.Since(w.start).Nanoseconds()
}

CountWatch定義了start屬性,它提供了Start、Reset、Count方法;其中Start方法判斷start爲初始值的時候設置爲time.Now;Reset設置start爲time.Now;Count計算當前時間距離start的納秒數

實例

func countWatchDemo() {
	var cw gxtime.CountWatch
	cw.Start()
	//do something
	fmt.Println("cost:%d ns", cw.Count())
}

先start,再通過count獲取時間間隔,若要繼續使用,則需要執行Reset

小結

gost的CountWatch定義了start屬性,它提供了Start、Reset、Count方法;其中Start方法判斷start爲初始值的時候設置爲time.Now;Reset設置start爲time.Now;Count計算當前時間距離start的納秒數。

doc

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章