panic: time: missing Location in call to Time.In

起因

在处理与时间验证相关的代码时,特意指定了time的location, 在本地运行通过,放到某个docker容器中直接报:panic: time: missing Location in call to Time.In错误

分析

主要代码
省去业务相关代码,只保留最关键的一个

loc, _ := time.LoadLocation("Asia/Chongqing")
return time.Now().In(loc)

官方文档
// LoadLocation returns the Location with the given name.
//
// If the name is “” or “UTC”, LoadLocation returns UTC.
// If the name is “Local”, LoadLocation returns Local.
//
// Otherwise, the name is taken to be a location name corresponding to a file
// in the IANA Time Zone database, such as “America/New_York”.
//
// The time zone database needed by LoadLocation may not be
// present on all systems, especially non-Unix systems.
// LoadLocation looks in the directory or uncompressed zip file
// named by the ZONEINFO environment variable, if any, then looks in
// known installation locations on Unix systems,
// and finally looks in $GOROOT/lib/time/zoneinfo.zip.

时间的位置信息是从本地文件系统中提取的,大多数系统都支持它,但还是取决于当前系统。具体的配置存储路径:

$ cd /usr/share/zoneinfo

进入该目录就可以看到各时区的配置信息

解决办法

  1. 在base 容器中添加上相关时区信息
    示例 Dockerfile
FROM golang:1.12-alpine as build_base
RUN apk add --update bash make git
WORKDIR /go/src/github.com/your_repo/your_app/
ENv GO111MODULE on
COPY go.mod .
COPY go.sum .
RUN go mod download

FROM build_bash AS server_builder
COPY . ./
RUN GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /go/bin/your_app

FROM alpine
RUN apk add tzdata

# 自定义运行阶段的命令

最主要的就是增加了RUN apk add tzdata 这条命令

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值