聊聊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

  • gost

本文来自:Segmentfault

感谢作者:codecraft

查看原文:聊聊gost的CountWatch

相关阅读 >>

Golang如何接收输入

Golang 可以把包名去掉吗?

Golang 中的高级循环

Gocn酷Go推荐】Go程序配置利器-viper库

Golang使用kafka报错c.client.config.config.consumer.offsets.commitinterval undefined问题

手撸Golang etcd raft协议之3

手撸Golang 行为型设计模式 备忘录模式

[译]Golang 1.8工具链改进

Golang 如何结束一个协程

[Go]使用Go-smtp发送邮件通知

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...