Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i have custom build flags like os/arch comment directives

Tags:

go

In golang I am able to set compiler directives in the first line comment of a file to determine whether the code on that file is included in the build based on OS or arch, e.g

to target windows:

// +build windows

or non-windows:

// +build !windows

Is there any way to pass in my own boolean variable at build time to operate in the same way?

The background is that I would like a boolean debug flag which I can pass in to do a debug build, I don't want my debug code included in the normal build.

I currently do something like this:

go build -ldflags "-X main.Debug=true"

but I would prefer to use 1st line comment method particularly as this approach doesn't omit debug code from the build (I presume).

Ideally I want:

debug-on.go

// +build debug

package debug

func Debug() bool {
    return true
}

and debug-off.go

// +build !debug

package debug

func Debug() bool {
    return false
}

Update RE duplicate I accept the related question has the same subject matter but it's really not a duplicate question, that is a question from somebody who already knew about this feature but is struggling with implementation.

My 2 cents is that it seems the equivalent of "What language to people speak in France?" vs "How do you say 'This is not a duplicate' in French?"

like image 750
SwiftD Avatar asked Nov 12 '17 20:11

SwiftD


People also ask

What is a build flag?

Build Flags, used in combination with Build Profiles, allow you to generate different versions of the same Help System from a single project. The easiest way to understand how Build Flags can be used is to view the movie below or the written worked example of how they might be used.

What is build tag?

In Go, a build tag, or a build constraint, is an identifier added to a piece of code that determines when the file should be included in a package during the build process.

What are build tags golang?

In Go, a build tag is an indicator that is added to a piece of our code, to indicate when a package will be included in the building process.

What is Go build?

gobuild is an automatic build tool that aims to replace Makefiles for simple projects written in the Go programming language. It creates a dependency graph of all local imports and compiles them in the right order using the gc Go compiler.


Video Answer


1 Answers

Command go

Go is a tool for managing Go source code.

Usage:

go command [arguments]

Compile packages and dependencies

Usage:

go build [-o output] [-i] [build flags] [packages]

Build compiles the packages named by the import paths, along with their dependencies, but it does not install the results.

The build flags are shared by the build, clean, get, install, list, run, and test commands:

-tags 'tag list'
  a space-separated list of build tags to consider satisfied during the
  build. For more information about build tags, see the description of
  build constraints in the documentation for the `go/build` package.

go/build package: Build Constraints


For example, in source,

// +build debug

At runtime, for build, clean, get, install, list, run, and test go commands,

$ go install -tags='debug'
like image 121
peterSO Avatar answered Oct 10 '22 14:10

peterSO