douzheyo2617 2015-06-15 13:35
浏览 643
已采纳

Go上是否存在诸如#ifdef之类的C ++宏之类的东西,因此我可以选择基于标记构建什么?

I need to build in go for linux and windows with different packages same file on windows I need to import github.com/hashicorp/go-syslog and on linux import log/syslog

and inside code in file I have to use syslog.ALERT or gsyslog.ALERT depending on os. Does exist on Go something like macros in C++ like #ifdef so I can choose what to build based on flag ? How to achieve this without, same file to have different content for build based on OS ?

  • 写回答

2条回答 默认 最新

  • dounue1965 2015-06-15 13:44
    关注

    This is what build tags are for:

    A build constraint, also known as a build tag, is a line comment that begins

    // +build
    

    that lists the conditions under which a file should be included in the package. Constraints may appear in any kind of source file (not just Go), but they must appear near the top of the file, preceded only by blank lines and other line comments. These rules mean that in Go files a build constraint must appear before the package clause.

    Another way to distinguish OS/arch at runtime is to use runtime's GOOS and GOARCH constants:

    GOARCH is the running program's architecture target: 386, amd64, or arm.

    GOOS is the running program's operating system target: one of darwin, freebsd, linux, and so on.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教