栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

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

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 这条命令

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/991646.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号