如何创建包含 zoneinfo.zip 的二进制文件 [英] How to create a binary that contains zoneinfo.zip

查看:20
本文介绍了如何创建包含 zoneinfo.zip 的二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Docker 创建一个容器应用程序,然后将其部署到

解决方案

当使用 Go 的时间包,特别是 LoadLocation 方法时,它会寻找 时区数据库 各个位置的信息.这在 https://golang.org/src 源代码中的 LoadLocation 注释中进行了解释/time/zoneinfo.go.具体来说,它看起来在这些地方:

  1. 由 ZONEINFO 环境变量指定的位置
  2. 时区数据库文件在 Unix 操作系统上的已知位置
  3. $GOROOT 中的 zoneinfo.zip 文件

当您在 Windows 机器上编程时,Go 很可能默认使用第三个选项.但是,当您使用二进制文件时没有 $GOROOT,因此它在您的容器中不起作用.在大多数版本的 Linux 上,第二个选项可以正常工作,因为它们具有必要的时区数据库文件.但是,我强烈怀疑您引用的优化容器没有.这给您留下了选项 1,它主要包括将您自己的时区数据库文件放在容器上,然后使用 ZONEINFO 环境变量引用它们的位置.这既是一种痛苦,我还发现当您请求一个不存在的时区文件时,它会默默地失败.

在我自己解决这个问题时,我最终创建了一个使用 LoadLocationFromTZData 方法的包,并尝试简化提供和使用您自己的时区数据库副本的过程.你可以在这里看到我自己的堆栈溢出问题:检测 ZONEINFO 在 Go 中是否失败.您可以在此处查看我的时区包的存储库:https://github.com/slotheroo/knozone

I'm using Docker to create a container application and then deploy it to kubernetes engine but when the application is been initialized I get this error:

err: open C:Go/lib/time/zoneinfo.zip: no such file or directory

解决方案

When using Go's time package, specifically the LoadLocation method, it looks for time zone database information in various locations. This is explained in the comments for LoadLocation in the source code at https://golang.org/src/time/zoneinfo.go. Specifically it looks in these places:

  1. A location specified by a ZONEINFO environment variable
  2. Known locations where time zone database files are kept on Unix operating systems
  3. The zoneinfo.zip file in your $GOROOT

When you're programming on a Windows machine, Go is most likely defaulting to the 3rd option. However, there is no $GOROOT when you're working with a binary, so that won't work in your container. On most versions of Linux the 2nd option would work fine as they would have the necessary time zone database files. However, I have a strong suspicion that the optimized container you reference does not. This leaves you with option 1, which essentially consists of putting your own time zone database files on the container and then referencing their location with the ZONEINFO environment variable. This is both kind of a pain in the ass and I've also found that it fails silently when you request a time zone file that doesn't exist.

In my own addressing of this problem I ended up creating a package that utilizes the LoadLocationFromTZData method and tries to simplify the process of providing and working with your own copy of the time zone database. You can see my own stack overflow question here: Detect if ZONEINFO fails in Go. And you can see the repository for my time zone package here: https://github.com/slotheroo/knozone

这篇关于如何创建包含 zoneinfo.zip 的二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆