使用go run设置GOOS

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

Setting GOOS with go run

问题

我目前正在开发一个可以作为Windows服务构建或作为OSX/Linux可执行文件运行的服务。

我在Windows文件上使用了构建标签,包括一个包含主方法的文件:

// +build windows

还有另一个包含主方法的文件:

// +build !windows

当我在Mac上执行go run *.go时,我得到以下错误:

mainDOS.go:10:2: 在 /Users/michaelbrandenburg/Documents/git-repo/goCode/src/golang.org/x/sys/windows/svc 中没有可构建的Go源文件
windowsService.go:15:2: 在 /Users/michaelbrandenburg/Documents/git-repo/goCode/src/golang.org/x/sys/windows/svc/debug 中没有可构建的Go源文件
install.go:14:2: 在 /Users/michaelbrandenburg/Documents/git-repo/goCode/src/golang.org/x/sys/windows/svc/eventlog 中没有可构建的Go源文件
install.go:15:2: 在 /Users/michaelbrandenburg/Documents/git-repo/goCode/src/golang.org/x/sys/windows/svc/mgr 中没有可构建的Go源文件

有没有办法运行go run并针对我想要运行的架构进行目标设置?我可以顺利构建可执行文件。

英文:

I'm currently developing a service that can be built as windows service or run as OSX/linux executable.

I'm using build tags on windows files, including the one with a main method

// +build windows

And on the other file containing a main method

// +build !windows

When I execute go run *.go on the mac side, I get the following error

mainDOS.go:10:2: no buildable Go source files in /Users/michaelbrandenburg/Documents/git-repo/goCode/src/golang.org/x/sys/windows/svc
windowsService.go:15:2: no buildable Go source files in /Users/michaelbrandenburg/Documents/git-repo/goCode/src/golang.org/x/sys/windows/svc/debug
install.go:14:2: no buildable Go source files in /Users/michaelbrandenburg/Documents/git-repo/goCode/src/golang.org/x/sys/windows/svc/eventlog
install.go:15:2: no buildable Go source files in /Users/michaelbrandenburg/Documents/git-repo/goCode/src/golang.org/x/sys/windows/svc/mgr

Is there a way to run go run and target the architecture I want to run? I can build the executables with no problem.

答案1

得分: 2

GOOS=darwin go run *.go 将设置 Mac OSX 的环境。尽管如JimB所说,这没有太大意义。但是,使用 GOOS=darwin go build *.go 是一种很好的交叉编译方法。

英文:

GOOS=darwin go run *.go will set the env for Mac OSX. Though, like JimB said, there isn't much of a point. Doing GOOS=darwin go build *.go is a good way to cross compile though

huangapple
  • 本文由 发表于 2016年4月21日 02:07:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/36751815.html
匿名

发表评论

匿名网友

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

确定