gpt4 book ai didi

go - 为什么godoc不跳过golang中的例子?

转载 作者:IT王子 更新时间:2023-10-29 02:30:22 25 4
gpt4 key购买 nike

我正在尝试制作文档并提供示例,但我不知道该怎么做,因为 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%

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

最佳答案

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

godoc 示例的示例: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。这是有效的,因为我有一个类型称为(确切地说)Multiplexer。如果你有一个功能,你可以这样做,但名称需要匹配。

当使用后缀时,后缀以小写字母开头非常重要,否则 godoc 将丢弃它。

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

关于go - 为什么godoc不跳过golang中的例子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24519347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com