Skip to content

Instantly share code, notes, and snippets.

@vikyd
Last active January 16, 2019 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vikyd/d7c524e0db3bc7ca12723d37c1891130 to your computer and use it in GitHub Desktop.
Save vikyd/d7c524e0db3bc7ca12723d37c1891130 to your computer and use it in GitHub Desktop.
Golang build with custom tags, run `go build`, run `go build -tags abc`

go build directly

run:

go build

then run binary file, expected print:

def

go build with tags

run:

go build -tags abc

then run binary file, expected print:

abc

custom tags

You can change abc to aabbcc or others in both main_abc.go, main_def.go and go build -tags aabbcc.

used for go-sqlite3

In some case, you may want you Golang program supporting both MySQL and SQLite in seperate binaries.

With build tags option, you can build both MySQL and SQLite version binary with different build command without modify you Golang source code.

// +build abc
package main
import (
"fmt"
)
func main() {
fmt.Println("abc")
}
// +build !abc
package main
import (
"fmt"
)
func main() {
fmt.Println("def")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment