0

我试图制作文档并提供示例,但我不明白如何做,因为 godoc 跳过了我的示例。

当我进入localhost:8080/pkg/hello_example浏览器时,它会在终端上打印:

2014/07/01 15:54:29 skipping example 'ExampleAuthenticate' because 'Authenticate' is not a known function or type
2014/07/01 15:54:29 skipping example 'ExampleGetAllowAllDriver' because 'GetAllowAllDriver' is not a known function or type
2014/07/01 15:54:29 skipping example 'ExampleGetAndUseAllowAllDriver' because 'GetAndUseAllowAllDriver' is not a known function or type
2014/07/01 15:54:29 skipping example 'ExampleGetAndUseDenyAllDriver' because 'GetAndUseDenyAllDriver' is not a known function or type
2014/07/01 15:54:29 skipping example 'ExampleGetDenyAllDriver' because 'GetDenyAllDriver' is not a known function or type
2014/07/01 15:54:29 skipping example 'ExampleGetRedisDriver' because 'GetRedisDriver' is not a known function or type
2014/07/01 15:54:30 skipping example 'ExampleAuthenticate' because 'Authenticate' is not a known function or type
2014/07/01 15:54:30 skipping example 'ExampleGetAllowAllDriver' because 'GetAllowAllDriver' is not a known function or type
2014/07/01 15:54:30 skipping example 'ExampleGetAndUseAllowAllDriver' because 'GetAndUseAllowAllDriver' is not a known function or type
2014/07/01 15:54:30 skipping example 'ExampleGetAndUseDenyAllDriver' because 'GetAndUseDenyAllDriver' is not a known function or type
2014/07/01 15:54:30 skipping example 'ExampleGetDenyAllDriver' because 'GetDenyAllDriver' is not a known function or type
2014/07/01 15:54:30 skipping example 'ExampleGetRedisDriver' because 'GetRedisDriver' is not a known function or type
^C%

我不明白,所以我不能随意命名示例?这些名称是否必须与我的包或文件的函数名称匹配?这是在我的测试包中,所以我可能正在尝试在不同的包和文件中测试功能。我该怎么做呢?

4

1 回答 1

4

godoc 对名称非常挑剔,您的示例函数名称需要与实际函数名称、类型名称或其他相关联。请参阅http://golang.org/pkg/testing/#hdr-Examples以供参考。

godoc exmaple 示例:https ://godoc.org/github.com/creack/multio#example-Multiplexer--ReadWriter ,代码:https ://github.com/creack/multio/blob/master/example_test.go

您会注意到,示例被称为ExampleMultiplexer_simple ExampleMultiplexer_readWriter. 这是因为我想要两个例子,我可以简单地使用ExampleMultiplexer. 这是有效的,因为我有一个名为 (exactly) 的类型Multiplexer。如果你有一个函数,你也可以这样做,但名称需要匹配。

使用后缀时,后缀以小写开头是非常重要的,否则godoc会丢弃它。

所以回答你的问题:是的,你可以使用任意名称,但你仍然需要遵循 godoc 风格ExampleTypeName_suffix(小写后缀首字母)

于 2014-07-01T21:20:30.380 回答