在Bitbucket Pipelines中交叉编译Golang

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

Cross-compiling Golang in Bitbucket Pipelines

问题

我正在尝试配置Bitbucket Pipelines,以便自动将Golang代码编译为Linux、OSX和Windows。我正在使用Go的交叉编译功能来实现这一目标;流水线正在运行在Linux环境中,并通过设置GOOS和GOARCH的值来进行向OSX和Windows的交叉编译。然而,我无法使Windows构建成功-它报错并告诉我找不到某个包。Linux和OSX的构建都成功了。然而,Windows的构建失败了,告诉我它找不到/go/src/github.com/sirupsen/logrus/hooks/syslog。之前的两个构建都成功使用了这个包,并且在构建命令之前运行以下命令:

ls /go/src/github.com/sirupsen/logrus/hooks/syslog
ls ${GOPATH}/src/github.com/sirupsen/logrus/hooks/syslog

显示该文件夹中有两个Go文件。我怀疑可能是Windows文件系统与Linux文件系统之间的某些问题。我已经花了几个小时搜索和尝试,但似乎没有解决这个问题的办法。

英文:

I'm trying to configure Bitbucket Pipelines to automatically compile Golang code to Linux, OSX, and Windows. I'm using Go's cross-compile functionality for this; the pipeline is running a Linux environment, and cross-compiling to OSX and Windows by setting the values of GOOS and GOARCH. However, I can't get the Windows build to work- it errors and tells me that it can't find a certain package. Both the Linux and OSX builds succeed. However, the Windows build fails, telling me it can't find /go/src/github.com/sirupsen/logrus/hooks/syslog. Both of the previous builds used this package successfully, and running both of

ls /go/src/github.com/sirupsen/logrus/hooks/syslog
ls ${GOPATH}/src/github.com/sirupsen/logrus/hooks/syslog

the line before the build command displays two Go files in that folder. I suspect something might be wrong with maybe the windows filesystem trying to talk to the Linux filesystem? I've spent a few hours searching and experimenting, but nothing seems to resolve the issue.

答案1

得分: 1

syslog是一个特定于平台的服务,因此Logrus syslog hook代码有一个特殊的注释,称为构建标签,告诉Go工具在Windows(或Native Client或Plan9)上不要构建它:

// +build !windows,!nacl,!plan9

除非您不希望在Linux上集成syslog,否则您可能希望将当前始终导入hooks/syslog的应用程序代码分叉为两个版本,一个用于具有syslog的平台,另一个用于没有syslog的平台。如果当前导入hooks/syslog的文件是logconfig.go,您可以创建两个文件logconfig_syslog.gologconfig_nosyslog.gosyslog版本带有类似上面的约束条件,而nosyslog版本则相反(// +build windows,nacl,plan9)。

Dave Cheney在他的博客中更详细地介绍了构建标签和Go条件编译的各种变体。

英文:

syslog is a platform-specific service, so the Logrus syslog hook code has a special comment, called a build tag, to
tell the Go tools not to build it on Windows (or Native Client or Plan9):

// +build !windows,!nacl,!plan9

Unless you don't want syslog integration even on Linux, you probably want to fork the application code that currently always imports hooks/syslog into two versions, one for platforms with syslog, one for those without. If the file importing hooks/syslog is currently, say, logconfig.go, you could create two files logconfig_syslog.go and logconfig_nosyslog.go, the syslog version with a constraint like the one above, and the nosyslog version with the opposite (// +build windows,nacl,plan9).

Dave Cheney wrote a bit more about build tags and the various flavors of Go conditional compilation.

huangapple
  • 本文由 发表于 2017年6月23日 05:54:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/44709945.html
匿名

发表评论

匿名网友

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

确定