时间间隔,即 Duartion
类型, 业务也是很常用的类型。
// func ParseDuration(s string) (Duration, error)
tp, _ := time.ParseDuration("1.5s")
fmt.Println(tp.Truncate(1000), tp.Seconds(), tp.Nanoseconds())
func (d Duration) Hours() float64
func (d Duration) Minutes() float64
func (d Duration) Seconds() float64
func (d Duration) Nanoseconds() int64
func (d Duration) Round(m Duration) Duration // 四舍五入
func (d Duration) Truncate(m Duration) Duration // 向下取整
这里需要注意:
有时候需要进行类似time.Sleep(conf.ExpireTime * time.Millisecond)这样的操作,通过配置文件获取一个整数再乘以相应时间单位作为一个时间间隔,这么直接使用往往会报错。明明ExpireTime 也是int64类型可是还是会报错。这里需要通过time.Duration()进行转化
duration :=time.Duration(conf.ExpireTime)
time.Sleep(duration * time.Millisecond