duanleiming2014 2015-05-21 09:30
浏览 38
已采纳

在golang中从测试文件中设置变量

i'm trying to set a variable from my unit tests file

main_test.go

var testingMode bool = true

main.go

if testingMode == true {
  //use test database
} else {
  //use regular database
}

If I run "go test", this works fine. If I do "go build", golang complains that testingMode is not defined (which should be the case since tests aren't part of the program).

But it seems if I set the global variable in main.go, I'm unable to set it in main_test.

What's the correct way to about this?

  • 写回答

2条回答 默认 最新

  • dt56449492 2015-05-21 09:42
    关注

    Try this:

    Define your variable as global in main.go:

    var testingMode bool
    

    And then set it to be true in your test file main_test.go:

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

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换