英文:
How to test logging(log.Println) in Golang?
问题
如何在Golang中测试下面的代码?
package main
import "log"
func main() {
    log.Println("hello world")
}
请注意,测试Golang代码的一种常见方法是使用单元测试。您可以使用内置的testing包来编写和运行测试。以下是一个示例:
package main
import (
    "log"
    "testing"
)
func TestMain(t *testing.T) {
    log.Println("Running test...")
    main()
    // Add your assertions here
}
在上面的示例中,我们使用testing包创建了一个名为TestMain的测试函数。在该函数中,我们首先打印一条消息以指示测试正在运行,然后调用main函数。您可以在TestMain函数中添加适当的断言来验证代码的行为是否符合预期。
要运行测试,请在终端中导航到包含测试文件的目录,并运行以下命令:
go test
这将运行所有的测试函数并输出结果。
希望这可以帮助到您!如果您有任何其他问题,请随时问我。
英文:
How to test below the code in Golang?
package main
import "log"
func main() {
	log.Println("hello world")
}
答案1
得分: 0
你的程序将写入log包的标准记录器,该记录器将写入标准错误输出。编译并运行你的程序,查看标准错误输出中出现的内容。
英文:
Your program writes to the log package standard logger which writes to standard error. Compile and run your program and see what appears on standard error.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论