利用Go1.4的新特性,在go test时输出glog日志

来源:互联网 发布:智能手环功能原理 知乎 编辑:程序博客网 时间:2024/04/28 00:13

Go1.4 新增了一个功能:TestMain(m *testing.M)

利用这个函数,可以在*_test.go里面统一设置一次glog的参数,让go test的输出,也能包含glog的日志!


func TestMain(m *testing.M) {flag.Set("alsologtostderr", "true")flag.Set("log_dir", "/tmp")flag.Set("v", "3")flag.Parse()ret := m.Run()os.Exit(ret)}


这样可以避免在每个TestXXX函数的开头都去设置flag.


参考:

https://justinas.org/my-reason-to-be-excited-for-go-14/

https://www.andyjeffries.co.uk/25-tips-for-intermediate-git-users/

0 0
原创粉丝点击