GO语言使用build的依赖项

huangapple go评论82阅读模式
英文:

GO Language Dependencies using build

问题

我不认为只有我一个人会对此感到困惑。

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

import (
	"fmt"
	"time"
)

我使用了"go build myprogram.go"命令,得到了一个可以正常运行的二进制文件(因为我已经安装了Go)。

然而,如果其他人没有安装Go,他们似乎会遇到错误。

例如:

open c:\go\lib\time\zoneinfo.zip: 系统找不到指定的路径。
panic: time: 在调用 Time.In 时缺少 Location。

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

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

英文:

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

答案1

得分: 0

没有您代码的更完整示例,很难回答具体的问题,但是我通常使用以下方法生成独立的二进制文件:

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",
}

huangapple
  • 本文由 发表于 2016年12月7日 02:05:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/41001984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定