当我审查一个 Golang 项目时,我发现了一些这样的代码, now是 Unix timestap。每秒调用一次此函数:
//main.go
func PrevSlot(now int64) int64 {
// now = time.Now().Unix()
var blockInterval = int64(10)
result := int64((now-1)/blockInterval) * blockInterval // why result is same ?
plog.Println("PrevSlot:", int64(result), int64((now-1)/blockInterval), now)
return result
}
func main() {
ticker := time.NewTicker(1 * time.Second)
for {
<-ticker.C
now := time.Now().Unix()
PrevSlot(now)
}
}
输出:
2019-01-16 10:58:31.668597 I | dpos.go: PrevSlot: 1547607510 154760751 1547607511
2019-01-16 10:58:32.668649 I | dpos.go: PrevSlot: 1547607510 154760751 1547607512
2019-01-16 10:58:33.668568 I | dpos.go: PrevSlot: 1547607510 154760751 1547607513
2019-01-16 10:58:34.668572 I | dpos.go: PrevSlot: 1547607510 154760751 1547607514
2019-01-16 10:58:35.668547 I | dpos.go: PrevSlot: 1547607510 154760751 1547607515
结果是一样的。这是为什么,原理是什么?
青春有我
江户川乱折腾
相关分类