Golang Timezone Conversion Guide
In Go, the Time type’s In method from the time package can be used for timezone conversion. Here is a simple example code:
package main
import (
"fmt"
"time"
)
func main() {
// 创建一个时间对象
t := time.Now()
// 设置目标时区
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
fmt.Println(err)
return
}
// 进行时区转换
t = t.In(loc)
// 打印转换后的时间
fmt.Println(t)
}
In the example above, we initially created a time.Time object t with the current time, then loaded the target time zone “Asia/Shanghai” using the time.LoadLocation method, and finally performed the time zone conversion using t.In(loc).