Go runs after publishing the project, and reports an error time: missing Location in call to Time.In

go error time: missing Location in call to Time.In, use to record the error to solve.

wrong description

Time zone is used in go code

	const timezone = "Asia/Shanghai"
func TimeFormat(date time.Time, pattern string) string {
    
    
	location, err := time.LoadLocation(timezone)
	date.In(location)
	return date.Format(pattern)
}

At this time, some servers and computers will report an error when using time.LoadLocation: time: missing Location in call to Time.In
, because there are missing files or configurations.

solution

const timezone = "Asia/Shanghai"
func TimeFormat(date time.Time, pattern string) string {
    
    
	location, err := time.LoadLocation(timezone)
	if err != nil {
    
    
		location = time.FixedZone("CST", 8*3600) //替换上海时区方式
	}
	date.In(location)
	return date.Format(pattern)
}

Guess you like

Origin blog.csdn.net/weixin_43578304/article/details/130052623