dougan6402 2014-09-29 09:22
浏览 752

Go:类似于C的宏,用于编写测试代码

When writing test code, I do a lot of this

if (!cond) {
    t.Fatal("error message")
}

It's a bit tedious. So I'd like to achieve the following

CHECK(cond, "error message")

So I attempted this

func CHECK(t *testing.T, cond bool, fmt string, a ...interface{}) {
  if !cond {
    t.Fatal(fmt, a)
  }
}

If it were a C macro it would've worked perfectly. But in Go, the line number where the failure is is wrong.

Is there a fix for this?

  • 写回答

3条回答 默认 最新

  • doumi7861 2014-09-29 09:28
    关注

    Sadly you can't do that.

    A workaround would be to get the line / function yourself, something like the trace function from https://stackoverflow.com/a/25954534/145587.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!