Go 支持按照条件编译,具体来说它是通过 go/build包 里定义的tags和命名约定来让Go的包可以管理不同平台的代码 。

我们这里以下面这个开源项目为例,来看Go的按条件编译, 这个开源项目是把Go的os包进行了扩展。

https://bitbucket.org/kardianos/osext/src 

 

osext 是获得当前执行程序的执行目录和文件信息。

执行情况如下:

 

查看编译文件

我们用go list来查看在当前平台下 os/exec包里有哪些文件将会被编译

看当前项目下,有哪些文件会被编译,命令如下:

这里的 {{.GoFiles}} 是text/template里的模板代码

 

go list 命令的信息如下:
查看包名、路径、依赖项等等信息。
参数:
-json: 使用 json 格式输出包的相关信息,包括下面的依赖和导⼊。
-f{{.Deps}}: 查看依赖包,包括直接或间接依赖。
-f{{.Imports}}: 查看导入的包。

 

D:\mycodes\golang\src\bitbucket.org\kardianos\osext>go list -f
flag needs an argument: -f
usage: list [-e] [-f format] [-json] [build flags] [packages]

List lists the packages named by the import paths, one per line.

The default output shows the package import path:

    code.google.com/p/google-api-go-client/books/v1
    code.google.com/p/goauth2/oauth
    code.google.com/p/sqlite

The -f flag specifies an alternate format for the list, using the
syntax of package template.  The default output is equivalent to -f
'{{.ImportPath}}'. The struct being passed to the template is:

    type Package struct {
        Dir           string // directory containing package sources
        ImportPath    string // import path of package in dir
        ImportComment string // path in import comment on package statement
        Name          string // package name
        Doc           string // package documentation string
        Target        string // install path
        Goroot        bool   // is this package in the Go root?
        Standard      bool   // is this package part of the standard Go library?

        Stale         bool   // would 'go install' do anything for this package?

        Root          string // Go root or Go path dir containing this package

        // Source files
        GoFiles        []string // .go source files (excluding CgoFiles, TestGoF
iles, XTestGoFiles)
        CgoFiles       []string // .go sources files that import "C"
        IgnoredGoFiles []string // .go sources ignored due to build constraints
        CFiles         []string // .c source files
        CXXFiles       []string // .cc, .cxx and .cpp source files
        MFiles         []string // .m source files
        HFiles         []string // .h, .hh, .hpp and .hxx source files
        SFiles         []string // .s source files
        SwigFiles      []string // .swig files
        SwigCXXFiles   []string // .swigcxx files
        SysoFiles      []string // .syso object files to add to archive

        // Cgo directives
        CgoCFLAGS    []string // cgo: flags for C compiler
        CgoCPPFLAGS  []string // cgo: flags for C preprocessor
        CgoCXXFLAGS  []string // cgo: flags for C++ compiler
        CgoLDFLAGS   []string // cgo: flags for linker
        CgoPkgConfig []string // cgo: pkg-config names

        // Dependency information
        Imports []string // import paths used by this package
        Deps    []string // all (recursively) imported dependencies

        // Error information
        Incomplete bool            // this package or a dependency has an error
        Error      *PackageError   // error loading package
        DepsErrors []*PackageError // errors loading dependencies

        TestGoFiles  []string // _test.go files in package
        TestImports  []string // imports from TestGoFiles
        XTestGoFiles []string // _test.go files outside package
        XTestImports []string // imports from XTestGoFiles
    }

The template function "join" calls strings.Join.

The template function "context" returns the build context, defined as:

        type Context struct {
                GOARCH        string   // target architecture
                GOOS          string   // target operating system
                GOROOT        string   // Go root
                GOPATH        string   // Go path
                CgoEnabled    bool     // whether cgo can be used
                UseAllFiles   bool     // use files regardless of +build lines,
file names
                Compiler      string   // compiler to assume when computing targ
et paths
                BuildTags     []string // build constraints to match in +build l
ines
                ReleaseTags   []string // releases the current release is compat
ible with
                InstallSuffix string   // suffix to use in the name of the insta
ll dir
        }

For more information about the meaning of these fields see the documentation
for the go/build package's Context type.

The -json flag causes the package data to be printed in JSON format
instead of using the template format.

The -e flag changes the handling of erroneous packages, those that
cannot be found or are malformed.  By default, the list command
prints an error to standard error for each erroneous package and
omits the packages from consideration during the usual printing.
With the -e flag, the list command never prints errors to standard
error and instead processes the erroneous packages with the usual
printing.  Erroneous packages will have a non-empty ImportPath and
a non-nil Error field; other information may or may not be missing
(zeroed).

For more about build flags, see 'go help build'.

For more about specifying packages, see 'go help packages'.

 

编译标签

 

在源代码里添加标注,通常称之为编译标签( build tag) ,编译标签是在尽量靠近源代码文件顶部的地方用注释的方式添加
go build在构建一个包的时候会读取这个包里的每个源文件并且分析编译便签,这些标签决定了这个源文件是否参与本次编译

编译标签添加的规则(附上原文):

  1. a build tag is evaluated as the OR of space-separated options
  2. each option evaluates as the AND of its comma-separated terms
  3. each term is an alphanumeric word or, preceded by !, its negation
  • 编译标签由空格分隔的编译选项(options)以"或"的逻辑关系组成
  • 每个编译选项由逗号分隔的条件项以逻辑"与"的关系组成
  • 每个条件项的名字用字母+数字表示,在前面加!表示否定的意思

比如  https://bitbucket.org/kardianos/osext/src   这里的 osext_procfs.go 文件头上的 +build 部分就是。

这里标示 linux netbsd openbsd solaris 这些操作系统下会编译这个文件。

更复杂的编译标签 可以参考: http://blog.csdn.net/varding/article/details/12675971 

 

 

 

文件后缀

这个方法通过改变文件名的后缀来提供条件编译,这种方案比编译标签要简单,go/build可以在不读取源文件的情况下就可以决定哪些文件不需要参与编译

文件命名约定可以在go/build 包里找到详细的说明,简单来说如果你的源文件包含后缀:_$GOOS.go,那么这个源文件只会在这个平台下编译,_$GOARCH.go也是如此。这两个后缀可以结合在一起使用,但是要注意顺序:_$GOOS_$GOARCH.go,    不能反过来用:_$GOARCH_$GOOS.go

比如  https://bitbucket.org/kardianos/osext/src 这里的文件就是这个规则

osext_plan9.go        只会在 plan9 中编译。

osext_windows.go   只会在windows下编译。

 

更多请参考: http://blog.csdn.net/varding/article/details/12675971 

 

参考资料:

使用go build 进行条件编译
http://blog.csdn.net/varding/article/details/12675971

Go按照条件编译的更多相关文章

  1. C# 条件编译

    本文导读: C#的预处理器指令从来不会转化为可执行代码的命令,但是会影响编译过程的各个方面,常用的预处理器指令有#define.#undef.#if,#elif,#else和#endif等等,下面介绍 ...

  2. C#-#define条件编译

    本文导读: C#的预处理器指令从来不会转化为可执行代码的命令,但是会影响编译过程的各个方面,常用的预处理器指令有#define.#undef.#if,#elif,#else和#endif等等,下面介绍 ...

  3. 条件编译#if

    1.为什么需要条件编译 客户的需求在不停地发生变化,一会儿需要这个功能,一会儿不需要这个功能.我们可以使用条件编译来方便地裁剪功能. 2.条件编译语句#if 条件编译语句#if的形式是 #if exp ...

  4. 条件编译#if #ifdef

    近期由于一些莫名其妙的原因开始学c++,我觉得我哪天要是挂了也是被自己给折腾死的,算了,反正不是折腾死就是被淘汰,当是没事打发时间了,废话不多说,开始今天的主题. 之前接触的注释就是注释,条件语句就是 ...

  5. [AIR] as3 之条件编译多平台妙用

    http://bbs.9ria.com/thread-418864-1-1.html 一直希望as3 可以支持条件编译,即满足A时编译函数1,满足B时则编译函数2. 最佳百度了之后,发现原来是可以实现 ...

  6. 认识Visual Studio 条件编译

    一开始是在一些源代码中看到这些语法符号,了解到这就是" 条件编译"技术

  7. C# 条件编译备忘

    第一步:配置管理器中新建解决方案配置 第二步:定义条件编译符号: 第三步:在代码中使用自定义的条件编译 #if CustomDebug Console.WriteLine("dsads&qu ...

  8. Java条件编译

    学习过C语言或者C++语言的同学都知道它们支持条件编译,那么今天我们来学习下在Java语言中如何实现条件编译.Java语言本身没有提供条件编译,但是Java编译器对.java文件编译为.class文件 ...

  9. c#定义全局条件编译符号

    在"工程"上单机右键,"属性"--->"生成"--->"条件编译符号"后边的输入框中,输入自定义的条件编译变 ...

  10. 如何判断平台工具集去做条件编译(VC++目录、预处理器定义、$(PlatformToolsetVersion))

    作者:zyl910 从VS2010开始,提供了一个平台工作集(Platform ToolSet)选项用于配制vc编译版本.到了VS2012,更是因为默认平台工具集不支持WindowsXP,导致经常需要 ...

随机推荐

  1. 查看python中已安装的包

    pip list 现在我又知道了个:rpm -qa | grep XXXX(moudle name)

  2. java----session

    什么是session? 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),也就是说他是保存在服务端的.注意:一个浏览器独占一个session对象(默认情况下).因此,在 ...

  3. python 之 比较哪个数据大小

    #定义一个字典info={}#定义比较的人数n=int(input("请输入你要比较的人数"))#循环while(n): #输入a,b 两个数据 ,分别代表学号 和分数 # 把输入 ...

  4. nginx负载均衡之入门配置

    先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可以解释N台服务器平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况.那么负载均衡的前提就是要有多台服务器才能实现,也就是两台以上 ...

  5. 如何在本地连接服务器上的MySQL

    服务器以阿里云为例 1.首先确保防火墙开放了3306端口 2.确保服务器的linux系统防火墙开放了3306端口 firewall-cmd --list-ports # 查看端口 我这里已经启动了,如 ...

  6. 搜索引擎原理和SEO

    搜索引擎原理 通常是指收集了万维网上几千万到十几亿网页病对网页的每个词(即关键词)进行索引,建立搜索引擎数据库的全文搜索引擎. 当用户每次查询某个关键词的时候,所有在页面内容包含了该关键词的网页都作为 ...

  7. 解决FTPClient linux环境下FTPClient调用retrieveFileStream导致线程挂起(防火墙问题);下载文件小于实际文件问题

    FTPClient调用retrieveFileStream导致线程挂起(防火墙问题):下载文件小于实际文件问题解决 实际是因为FTP的两种传输模式:主动模式和被动模式的不同而导致的 FTPClient ...

  8. OpenERP 干掉 产品计量单位中的 search more 和 create and edit

    实际操作中特别容易点错而新建了重复的单位,通过下边的方法可以将“search more”和 “create and edit”干掉 在新继承product.product的模块中,修改xml文件 这样 ...

  9. Linux下which命令使用详解(转)

    我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which 查看可执行文件的位置. whereis 查看文件的位置. locate 配合数据库查看文件位置. f ...

  10. Mac下快速新建txt文件

    1.打开终端,定位到桌面 cd desktop 2.输入 vi test.txt 此时,一个txt文件就会建立在桌面上,操作vi时的提示:按[i]为输入内容,编辑好之后按[esc]键,然后输入[:wq ...