ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 概述 ### 测试 ``` func TestXxx(*testing.T) ``` ### 压测 ``` func BenchmarkXxx(*testing.B) ``` ### Examples 该包还运行并验证示例代码示例函数可以包括以 "Output:" 开头的行注释,并在运行测试时与函数的标准输出进行比较。 (比较时会忽略前导和尾随空格。) ``` func ExampleHello() { fmt.Println("hello") // Output: hello } func ExampleSalutations() { fmt.Println("hello, and") fmt.Println("goodbye") // Output: // hello, and // goodbye } ``` example 声明的命名约定:包,函数 F,类型 T,类型 T 上的方法 M 依次是 ``` func Example() { ... } func ExampleF() { ... } func ExampleT() { ... } func ExampleT_M() { ... } ``` 包/类型/函数/方法 提供多个 example 函数。后缀必须是以小写字母开头 ``` func Example_suffix() { ... } func ExampleF_suffix() { ... } func ExampleT_suffix() { ... } func ExampleT_M_suffix() { ... } ``` 当一个文件包含一个示例函数,同时至少一个其他函数,类型,变量或常量声明,或没有测试或基准函数时,这个测试文件作为示例存在,通常命名为 example\_test.go