dongpan5289 2016-06-02 08:44
浏览 30
已采纳

如果文件运行在go 1.4及更低版本的系统上,如何跳过该文件?

I have a file containing some tests that should be run on Go 1.5+.

I am able to get the Go runtime version using runtime.Version() and doing various comparisons.

However, the test file imports golang.org/x/net/http2. The http2 package requires request.Cancel() from net/http, but that is only available on Go 1.5+.

That causes these errors in my CI environment causing the build to fail:

../../../golang.org/x/net/http2/transport.go:214: req.Cancel undefined (type *http.Request has no field or method Cancel)
../../../golang.org/x/net/http2/transport.go:218: req.Cancel undefined (type *http.Request has no field or method Cancel)
../../../golang.org/x/net/http2/transport.go:777: req.Cancel undefined (type *http.Request has no field or method Cancel)

I tried adding // +build go1.5 to the top of the file, but it didn't work.

Is there anyway I can limit a unit test file so that it is built and tested only on Go 1.5+ systems?

  • 写回答

1条回答 默认 最新

  • douzai1074 2016-06-02 09:16
    关注

    The build constraints is the proper way to do it.

    But note that your error messages refer to the http2 package which was added in Go 1.6, so you need at least go1.6 build constraint.

    The build constraint

    // +build go1.5
    

    Will cause the file to be compiled with Go 1.5 and onward. So if you want your test file to only compile and run with Go 1.6 and above, then use

    // +build go1.6
    

    Also don't forget that:

    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.

    To distinguish build constraints from package documentation, a series of build constraints must be followed by a blank line.

    A working example:

    1  // +build go1.6
    2
    3  package yourpackage
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗