dth42345 2016-03-08 00:17
浏览 64
已采纳

我可以基于要构建的操作系统导入Golang软件包吗?

Say I have a go project that based on which OS, and in some cases which distro, I want to use say a Systemd client package vs an Upstart client package vs a sysv client package vs a launchd client package. Is it possible to selectively import each package so I only import the one I need per OS/distro I'm building for? Or do I have to import each package for each OS/distro?

  • 写回答

1条回答 默认 最新

  • dqd22496 2016-03-08 00:23
    关注

    Package build

    Build Constraints

    A build constraint, also known as a build tag, is a line comment that begins

    // +build
    

    that lists the conditions under which a file should be included in the package. Constraints may appear in any kind of source file (not just Go), but they must appear near the top of the file, preceded only by blank lines and other line comments. These rules mean that in Go files a build constraint must appear before the package clause.

    To distinguish build constraints from package documentation, a series of build constraints must be followed by a blank line.

    A build constraint is evaluated as the OR of space-separated options; each option evaluates as the AND of its comma-separated terms; and each term is an alphanumeric word or, preceded by !, its negation. That is, the build constraint:

    // +build linux,386 darwin,!cgo
    

    corresponds to the boolean formula:

    (linux AND 386) OR (darwin AND (NOT cgo))
    

    A file may have multiple build constraints. The overall constraint is the AND of the individual constraints. That is, the build constraints:

    // +build linux darwin
    // +build 386
    

    corresponds to the boolean formula:

    (linux OR darwin) AND 386
    

    During a particular build, the following words are satisfied:

    - the target operating system, as spelled by runtime.GOOS
    - the target architecture, as spelled by runtime.GOARCH
    - the compiler being used, either "gc" or "gccgo"
    - "cgo", if ctxt.CgoEnabled is true
    - "go1.1", from Go version 1.1 onward
    - "go1.2", from Go version 1.2 onward
    - "go1.3", from Go version 1.3 onward
    - "go1.4", from Go version 1.4 onward
    - "go1.5", from Go version 1.5 onward
    - "go1.6", from Go version 1.6 onward
    - any additional words listed in ctxt.BuildTags
    

    If a file's name, after stripping the extension and a possible _test suffix, matches any of the following patterns:

    *_GOOS
    *_GOARCH
    *_GOOS_GOARCH
    

    (example: source_windows_amd64.go) where GOOS and GOARCH represent any known operating system and architecture values respectively, then the file is considered to have an implicit build constraint requiring those terms (in addition to any explicit constraints in the file).

    To keep a file from being considered for the build:

    // +build ignore
    

    (any other unsatisfied word will work as well, but “ignore” is conventional.)

    To build a file only when using cgo, and only on Linux and OS X:

    // +build linux,cgo darwin,cgo
    

    Such a file is usually paired with another file implementing the default functionality for other systems, which in this case would carry the constraint:

    // +build !linux,!darwin !cgo
    

    Naming a file dns_windows.go will cause it to be included only when building the package for Windows; similarly, math_386.s will be included only when building the package for 32-bit x86.

    Using GOOS=android matches build tags and files as for GOOS=linux in addition to android tags and files.

    Use build constraints.

    Use a single package with multiple files. Each file specializes for a particular OS, architecture, etc, combination.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!