duanjunao9348 2017-11-12 20:53
浏览 53
已采纳

我可以使用os / arch注释指令等自定义生成标志吗

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?"

  • 写回答

1条回答 默认 最新

  • duanreng3439 2017-11-12 21:27
    关注

    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'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。