How to build differently for Linux vs Windows

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

How to build differently for Linux vs Windows

问题

我正在研究Go语言。
我有一个问题:例如,我将创建一个新的库,该库应该在Windows上使用一个Go包的方法,在Linux上使用另一个Go包的方法。

我只是想问,是否有一种方便的方式来组织构建过程?
当然,我可以为每个操作系统创建一个项目,并为每个操作系统更改导入名称。

英文:

I'm taking look on Go language.
And I have a question: for example I will create a new library, that should use methods from one Go package for Windows and from other for Linux.

I'm just asking, is there a convenient way of organizing build process?
Of course, I can just make a project for every OS and change import name for every OS.

答案1

得分: 6

使用构建约束和文件名。请参阅Package buildPackage os的源代码中有许多示例:https://golang.org/src/os/。Go源代码:https://go.googlesource.com/go 或 https://github.com/golang/go

Unix的构建约束:

// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris

一些构建文件名:

stat_darwin.go     stat_linux.go   stat_openbsd.go  stat_unix.go
stat_dragonfly.go  stat_nacl.go    stat_plan9.go    stat_windows.go
stat_freebsd.go    stat_netbsd.go  stat_solaris.go

Go工具和标准库最初使用构建文件名,随着需求变得更加复杂,开始使用构建约束。

英文:

Use build constraints and file names. See Package build. The source code for Package os has many examples: https://golang.org/src/os/. Go Source Code: https://go.googlesource.com/go or https://github.com/golang/go.

A build contraint for Unix:

// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris

Some build file names:

stat_darwin.go     stat_linux.go   stat_openbsd.go  stat_unix.go
stat_dragonfly.go  stat_nacl.go    stat_plan9.go    stat_windows.go
stat_freebsd.go    stat_netbsd.go  stat_solaris.go

The Go tools and standard library started out using build file names and then, as the requirements became more complex, began using build constraints.

huangapple
  • 本文由 发表于 2017年8月28日 09:38:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/45910807.html
匿名

发表评论

匿名网友

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

确定