GO语言依赖使用构建(GO Language Dependencies using build)

编程入门 行业动态 更新时间:2024-10-24 15:14:20
GO语言依赖使用构建(GO Language Dependencies using build)

我不认为我是唯一可能被这个困惑的人。

我有一个非常简单的Go程序,唯一的依赖是。

import ( "fmt" "time" )

我使用“去建立myprogram.go”,并得到一个二进制运行良好(因为我已经安装)

但是,如果其他人没有安装GO,他们似乎会得到错误。

例如:

open c:\go\lib\time\zoneinfo.zip: The system cannot find the path specified. panic: time: missing Location in call to Time.In

我需要做什么才能在构建中包含第三方库?

我想生成一个二进制文件,可以在任何平台上运行,而不用担心依赖关系

I don't think I'm the only one that might be confused by this.

I have a very simple Go Program, the only dependencies are.

import ( "fmt" "time" )

I used "go build myprogram.go" , and get a binary that runs fine (since I have GO installed)

However, if someone else does not have GO installed they seem to get errors.

For Example:

open c:\go\lib\time\zoneinfo.zip: The system cannot find the path specified. panic: time: missing Location in call to Time.In

What do I need to do to include third party libraries in the build ?

I'd like to generate a Binary that will run on any Platform without worrying about dependencies

最满意答案

如果没有你的代码的更完整的例子,很难回答具体问题,但我做什么来生成独立的二进制文件是:

CGO_ENABLED=0 go build -v -a -ldflags '-s -w' ./...

其中至少构建了一个没有libc依赖关系的静态二进制文件......似乎zoneinfo.zip依赖关系不会被覆盖。

zoneinfo.zip文件对于Go ...并不是唯一的,Go期望它被安装在系统上。 以下是扫描该文件的系统文件夹的代码: https : //golang.org/src/time/zoneinfo_unix.go#L31

写作时:

var zoneDirs = []string{ "/usr/share/zoneinfo/", "/usr/share/lib/zoneinfo/", "/usr/lib/locale/TZ/", runtime.GOROOT() + "/lib/time/zoneinfo.zip", }

Without a fuller example of your code, it's hard to answer the specific issue, but what I do to generate standalone binaries is:

CGO_ENABLED=0 go build -v -a -ldflags '-s -w' ./...

Which builds a static binary without libc dependencies at least... It seems the zoneinfo.zip dependency wouldn't be covered by that.

The zoneinfo.zip file is not unique to Go... Go expects it to be installed on the system. Here is the code that scans system folders for that file: https://golang.org/src/time/zoneinfo_unix.go#L31

At time of writing:

var zoneDirs = []string{ "/usr/share/zoneinfo/", "/usr/share/lib/zoneinfo/", "/usr/lib/locale/TZ/", runtime.GOROOT() + "/lib/time/zoneinfo.zip", }

更多推荐

本文发布于:2023-07-15 18:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117513.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语言   Language   build   Dependencies

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!