Golang构建多平台问题

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

Golang Building Multi Platform Issue

问题

Golang构建多平台问题

我正在构建一个Go应用程序,希望能够在Linux和Windows上构建。对于Windows部分,我希望它能够安装为Windows服务。因此,在我的应用程序中,我包含了以下包:

golang.org/x/sys/windows/svc
golang.org/x/sys/windows/svc/debug
golang.org/x/sys/windows/svc/eventlog
golang.org/x/sys/windows/svc/mgr

它在Windows上构建得很好,并且服务安装没有问题。但是当我尝试在Linux上构建时:

GOOS=linux GOARCH=amd64 go build -o app-amd64-linux

package github.com/user/app
    imports golang.org/x/sys/windows/svc: build constraints exclude all Go files in 
C:\Users\User\go\pkg\mod\golang.org\x\sys@v0.0.0-20220728004956-3c1f35247d10\windows\svc\package github.com/user/app
    imports golang.org/x/sys/windows/svc/debug: build constraints exclude all Go files in 
C:\Users\User\go\pkg\mod\golang.org\x\sys@v0.0.0-20220728004956-3c1f35247d10\windows\svc\debug\package github.com/user/app
    imports golang.org/x/sys/windows/svc/eventlog: build constraints exclude all Go files in 
C:\Users\User\go\pkg\mod\golang.org\x\sys@v0.0.0-20220728004956-3c1f35247d10\windows\svc\eventlog\package github.com/user/app
    imports golang.org/x/sys/windows/svc/mgr: build constraints exclude all Go files in 
C:\Users\User\go\pkg\mod\golang.org\x\sys@v0.0.0-20enter code here220728004956-3c1f35247d10\windows\svc\mgr

在代码中,我正在检查并且只有在应用程序作为Windows服务运行时才使用这些包。有没有办法忽略这些错误?或者只在构建Windows版本时导入它们?或者在go.mod中做一些更改,只在Windows上需要这些包?

英文:

Golang Building Multi Platform Issue

I'm building a go application that I want to build for both Linux and Windows. For the Windows piece, I would like it to have the ability to install as a Windows Service. So in my app, I've included the following packages:

golang.org/x/sys/windows/svc
golang.org/x/sys/windows/svc/debug
golang.org/x/sys/windows/svc/eventlog
golang.org/x/sys/windows/svc/mgr

It builds fine for Windows and the service installs with no issues. But when I try to build it for linux:

GOOS=linux GOARCH=amd64 go build -o app-amd64-linux

package github.com/user/app
imports golang.org/x/sys/windows/svc: build constraints exclude all Go files in 
C:\Users\User\go\pkg\mod\golang.org\x\sys@v0.0.0-20220728004956-3c1f35247d10\windows\svc\package github.com/user/app
imports golang.org/x/sys/windows/svc/debug: build constraints exclude all Go files in 
C:\Users\User\go\pkg\mod\golang.org\x\sys@v0.0.0-20220728004956-3c1f35247d10\windows\svc\debug\package github.com/user/app
imports golang.org/x/sys/windows/svc/eventlog: build constraints exclude all Go files in 
C:\Users\User\go\pkg\mod\golang.org\x\sys@v0.0.0-20220728004956-3c1f35247d10\windows\svc\eventlog\package github.com/user/app
imports golang.org/x/sys/windows/svc/mgr: build constraints exclude all Go files in 
C:\Users\User\go\pkg\mod\golang.org\x\sys@v0.0.0-20enter code here220728004956-3c1f35247d10\windows\svc\mgr

In the code, I'm checking and only using these packages if the app is running as a windows service. Is there a way ignore these errors? Or only import them when building for Windows? Or maybe something I can change in go.mod to only require those for windows?

答案1

得分: 1

作为解决方法,您可以使用构建约束(Build Constraints):

https://pkg.go.dev/go/build#hdr-Build_Constraints

Tim Cooper在这个帖子中详细解释了如何实现这些约束:

main.go

package main
func main() {
println("main()")
conditionalFunction()
}

a.go

// +build COMPILE_OPTION
package main
func conditionalFunction() {
println("conditionalFunction")
}

b.go

// +build !COMPILE_OPTION
package main
func conditionalFunction() {
}

输出:

*% go build -o example ; ./example
main()
% go build -o example -tags COMPILE_OPTION ; ./example
main()
conditionalFunction*

我一字不差地复制了这个答案,以免丢失信息。如果有人发现有问题,请纠正我。

英文:

As a workaround you might use Build Constrants:

https://pkg.go.dev/go/build#hdr-Build_Constraints

Tim Cooper made in this post an elaborate answer how to implement those:

> main.go

package main
func main() {
println("main()")
conditionalFunction()
}

> a.go

// +build COMPILE_OPTION
package main
func conditionalFunction() {
println("conditionalFunction")
}

> b.go

// +build !COMPILE_OPTION
package main
func conditionalFunction() {
}

> Output:

*% go build -o example ; ./example
main()
% go build -o example -tags COMPILE_OPTION ; ./example
main()
conditionalFunction*

I copied the answer one-to-one in order not to lose it. Somebody might correct me if this ain't wished.

huangapple
  • 本文由 发表于 2022年11月2日 21:17:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/74289819.html
匿名

发表评论

匿名网友

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

确定