建专业外贸网站,wordpress专用空间,网站广告如何做,网站免费正能量直接进入#x1f308;Don’t worry , just coding! 内耗与overthinking只会削弱你的精力#xff0c;虚度你的光阴#xff0c;每天迈出一小步#xff0c;回头时发现已经走了很远。
#x1f4d7;概念 在 Go 语言中#xff0c;time.Timer 是一个用于在指定时间后执行操作的计时器。…
Don’t worry , just coding! 内耗与overthinking只会削弱你的精力虚度你的光阴每天迈出一小步回头时发现已经走了很远。
概念 在 Go 语言中time.Timer 是一个用于在指定时间后执行操作的计时器。它可以用于延迟执行某些任务或在特定时间间隔内执行操作。 代码
日期Example
package mainimport (fmttime //time用于处理时间和日期。
)func main() {//p : fmt.Println将 fmt.Println 函数赋值给变量 p方便后续调用。p : fmt.Println//获取当前时间time.Now() 返回当前的本地时间。now : time.Now()p(now) //打印当前时间//创建一个特定的时间time.Date 创建一个指定的时间2009年11月17日20时34分58秒。then : time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)p(then) //打印这个特定的时间。//分别获取then的年月日时分秒p(then.Year())p(then.Month())p(then.Day())p(then.Hour())p(then.Minute())p(then.Second())p(then.Nanosecond()) //获取纳秒p(then.Location()) //获取时区信息p(then.Weekday()) //获取星期几p(then.Before(now)) //检查 then 是否在 now 之前。p(then.After(now)) //检查 then 是否在 now 之后。p(then.Equal(now)) //检查 then 是否与 now 相等。diff : now.Sub(then) //计算时间差now.Sub(then) 返回 now 和 then 之间的时间差time.Duration 类型。p(diff)//diff.Hours()返回时间差的小时数。//diff.Minutes()返回时间差的分钟数。//diff.Seconds()返回时间差的秒数。//diff.Nanoseconds()返回时间差的纳秒数。p(diff.Hours())p(diff.Minutes())p(diff.Seconds())p(diff.Nanoseconds())//then.Add(diff)在 then 时间上加上时间差 diff得到一个新的时间。//then.Add(-diff)在 then 时间上减去时间差 diff得到一个新的时间。p(then.Add(diff))p(then.Add(-diff))
}//输出
//2024-11-12 15:57:23.595423 0800 CST m0.000228772
//2009-11-17 20:34:58.651387237 0000 UTC
//2009
//November
//17
//20
//34
//58
//651387237
//UTC
//Tuesday
//true
//false
//false
//131363h22m24.944035763s
//131363.3735955655
//7.88180241573393e06
//4.7290814494403577e08
//472908144944035763
//2024-11-12 07:57:23.595423 0000 UTC
//1994-11-23 09:12:33.707351474 0000 UTC定时器Example
package mainimport (fmttime
)func main() {// 创建一个新的计时器设置为 2 秒timer : time.NewTimer(2 * time.Second)fmt.Println(Timer started for 2 seconds...)// 等待计时器到期-timer.Cfmt.Println(Timer expired!)// 创建另一个计时器timer2 : time.NewTimer(3 * time.Second)go func() {// 等待计时器到期并打印-timer2.Cfmt.Println(Timer 2 expired!)}()// 在 1 秒后停止计时器time.Sleep(1 * time.Second)stopped : timer2.Stop()if stopped {fmt.Println(Timer 2 stopped before expiration.)}// 等待一段时间以确保程序不会立即退出time.Sleep(5 * time.Second)
}//输出
//Timer started for 2 seconds...
//Timer expired!
//Timer 2 stopped before expiration.
EpochUnix纪元 Example
package mainimport (fmttime
)func main() {//获取当前时间time.Now() 返回当前的本地时间并将其赋值给变量 now。now : time.Now()fmt.Println(now)//now.Unix() 返回从 1970 年 1 月 1 日 00:00:00 UTC 到当前时间的秒数Unix 时间戳。fmt.Println(now.Unix())//now.UnixMilli() 返回从 1970 年 1 月 1 日 00:00:00 UTC 到当前时间的毫秒数。fmt.Println(now.UnixMilli())//now.UnixNano() 返回从 1970 年 1 月 1 日 00:00:00 UTC 到当前时间的纳秒数。fmt.Println(now.UnixNano())//time.Unix(now.Unix(), 0) 将当前时间的 Unix 时间戳秒转换回 time.Time 类型。fmt.Println(time.Unix(now.Unix(), 0)) //第二个参数 0 表示纳秒部分这里设置为 0。//time.Unix(0, now.UnixNano()) 将当前时间的 Unix 时间戳纳秒转换回 time.Time 类型。fmt.Println(time.Unix(0, now.UnixNano())) //第一个参数 0 表示秒部分这里设置为 0。
}//输出
//2024-11-12 16:14:18.384693 0800 CST m0.000127501
//1731399258
//1731399258384
//1731399258384693000
//2024-11-12 16:14:18 0800 CST
//2024-11-12 16:14:18.384693 0800 CST Tips小知识点
使用Time相关的注意事项
明确时区Go 默认使用 UTC 时间。确保在处理时区时明确指定特别是在涉及用户本地时间和服务器时间的场景中。使用 time.LoadLocation可以加载特定的时区
loc, err : time.LoadLocation(Asia/Shanghai)
if err ! nil {// 处理错误
}
选择合适的时间戳根据需求选择使用秒、毫秒或纳秒。Unix() 返回秒UnixMilli() 返回毫秒UnixNano() 返回纳秒。确保你在存储或比较时间时使用一致的单位。避免精度丢失在进行时间计算时注意可能的精度丢失尤其是在转换不同时间单位时使用 time.Duration使用 time.Duration 类型来表示时间间隔避免手动计算时间差计时器和Ticker的并发使用在并发环境中使用 time.Timer 和 time.Ticker 时要确保对它们的访问是安全的尤其是在多个协程中使用时。避免频繁调用 time.Now()在性能敏感的代码中尽量减少对 time.Now() 的频繁调用可以将结果缓存到变量中。
无人扶我青云志我自踏雪至山巅。