repo/common/logger.go
package common
var once sync.Once
var Logger *logrus.Logger
func InitLogger() {
once.Do(func() {
Logger = logrus.New()
Logger.Out = filename // file opened and *file assigned here for logging
})
return Logger
}
回购/setup_test.go
package main
func setUp() {
common.InitLogger()
fmt.Println(common.Logger) // prints some pointer related things which means logger is initialized
}
func TestMain(m *testing.M) {
fmt.Println("Starting Test...")
setUp()
code := m.Run()
common.APILog.Println("Finishing Main...")
os.Exit(code)
}
回购/车辆/品牌/maruti/car/mycar_test.go
package car
func TestMyFunc(t *testing.T) {
**t.Log(common.Logger) // When i run go test ./... this Prints nil , Why ?**
// Doing some tests
}
由于这个 common.Logger.Println("some data") 到处都给出零指针错误根据我的理解,一旦 Logger 被初始化,它应该是可用的,因为它是一个全局变量。我肯定在这里遗漏了一些微妙的东西。请帮忙