go build -tags 試驗

使用方法

  1. 構建約束以一行+build開始的注釋。在+build之后列出了一些條件,在這些條件成立時,該文件應包含在編譯的包中;
  2. 約束可以出現在任何源文件中,不限于go文件;
  3. +build必須出現在package語句之前,+build注釋之后應要有一個空行。
// 
// +build debug

package main

import "fmt"

func main() {
 fmt.Println("Hello World!")
}

語法規則

1)只允許是字母數字或_

2)多個條件之間,空格表示OR;逗號表示AND;嘆號(!)表示NOT

3)一個文件可以有多個+build,它們之間的關系是AND。如:

// +build linux darwin
// +build 386
等價于
// +build (linux OR darwin) AND 386

4)預定義了一些條件:
runtime.GOOS、runtime.GOARCH、compiler(gc或gccgo)、cgo、context.BuildTags中的其他單詞

5)如果一個文件名(不含后綴),以 *_GOOS, *_GOARCH, 或 *_GOOS_GOARCH結尾,它們隱式包含了 構建約束

6)當不想編譯某個文件時,可以加上// +build ignore。這里的ignore可以是其他單詞,只是ignore更能讓人知道什么意思

更多詳細信息,可以查看go/build/build.go文件中shouldBuildmatch方法。

應用實例1

除了*_GOOS這種預定義的應用,我們看一個實際的應用。

比如,項目中需要在測試環境輸出Debug信息,一般通過一個變量(或常量)來控制是測試環境還是生產環境,比如:if DEBUG {},這樣在生產環境每次也會進行這樣的判斷。在golang-nuts郵件列表中有人問過這樣的問題,貌似沒有討論出更好的方法(想要跟C中條件編譯一樣的效果)。下面我們采用Build constraints來實現。

1)文件列表:main.go logger_debug.go logger_product.go

2)在main.go中簡單的調用Debug()方法。

3)在logger_product.go中的Debug()是空實現,但是在文件開始加上// + build !debug

4)在logger_debug.go中的Debug()是需要輸出的調試信息,同時在文件開始加上// + build debug

這樣,在測試環境編譯的時傳遞-tags參數:go build/install -tags “debug” logger。生產環境:go build/install logger就行了。

對于生產環境,不傳遞-tags時,為什么會編譯logger_product.go呢?因為在go/build/build.go中的match方法中有這么一句:

if strings.HasPrefix(name, "!") { // negation
    return len(name) > 1 && !ctxt.match(name[1:])
}

也就是說,只要有!(不能只是!),tag不在BuildTags中時,總是會編譯。

應用實例2

本例程中,編譯的tag差異較大,兩個文件中一個是hash tag,一個是int tag,需要引入第三個tag來區分編譯的文件。否則,只要不帶!的tag都會被編譯進包。

display_hash.go

// +build hash !display_alternatives

// 上面
package main

import "fmt"

type DisplayName string

func Print(name DisplayName) {
    fmt.Printf("%s\n", name)
}

func MakeDisplayName(name string) DisplayName {
    return DisplayName(name)
}

display_int.go

// +build int

package main

import (
    "fmt"
    "encoding/hex"
    "encoding/binary"
)

type DisplayName uint64

func Print(name DisplayName) {
    fmt.Printf("%d\n", name)
}

func MakeDisplayName(name string) DisplayName {
    h, err := hex.DecodeString(name)
    if err != nil {
        panic(fmt.Sprintf("decode hex string failed. cause: %v\n", err))
    }
    fmt.Printf("data: %v\n", h)

    value := binary.BigEndian.Uint16(h)
    return DisplayName(value)
}

build_tag.go

package main

import (
    "sync"
    "fmt"
    "math/rand"
    "time"
)

func main() {
    var name DisplayName
    name = MakeDisplayName("FAD9C812")
    Print(name)

編譯display_int.go
編譯執行過程 go build -tags "display_alternatives int"

編譯display_hash.go
編譯執行過程 go build -tags hash

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。