为什么godoc在golang中不跳过示例?

huangapple go评论80阅读模式
英文:

Why doesn't godoc skip examples in golang?

问题

我正在尝试制作文档并提供示例,但我不知道如何做,因为godoc跳过了我的示例。

当我在浏览器中访问localhost:8080/pkg/hello_example时,终端上会打印出以下内容:

2014/07/01 15:54:29 跳过示例'ExampleAuthenticate',因为'Authenticate'不是已知的函数或类型
2014/07/01 15:54:29 跳过示例'ExampleGetAllowAllDriver',因为'GetAllowAllDriver'不是已知的函数或类型
2014/07/01 15:54:29 跳过示例'ExampleGetAndUseAllowAllDriver',因为'GetAndUseAllowAllDriver'不是已知的函数或类型
2014/07/01 15:54:29 跳过示例'ExampleGetAndUseDenyAllDriver',因为'GetAndUseDenyAllDriver'不是已知的函数或类型
2014/07/01 15:54:29 跳过示例'ExampleGetDenyAllDriver',因为'GetDenyAllDriver'不是已知的函数或类型
2014/07/01 15:54:29 跳过示例'ExampleGetRedisDriver',因为'GetRedisDriver'不是已知的函数或类型
2014/07/01 15:54:30 跳过示例'ExampleAuthenticate',因为'Authenticate'不是已知的函数或类型
2014/07/01 15:54:30 跳过示例'ExampleGetAllowAllDriver',因为'GetAllowAllDriver'不是已知的函数或类型
2014/07/01 15:54:30 跳过示例'ExampleGetAndUseAllowAllDriver',因为'GetAndUseAllowAllDriver'不是已知的函数或类型
2014/07/01 15:54:30 跳过示例'ExampleGetAndUseDenyAllDriver',因为'GetAndUseDenyAllDriver'不是已知的函数或类型
2014/07/01 15:54:30 跳过示例'ExampleGetDenyAllDriver',因为'GetDenyAllDriver'不是已知的函数或类型
2014/07/01 15:54:30 跳过示例'ExampleGetRedisDriver',因为'GetRedisDriver'不是已知的函数或类型
^C%

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

英文:

I was trying to make a documentation and provide examples but I didn't understand how to because godoc was skipping my examples.

when I go to localhost:8080/pkg/hello_example on the browser it print on the terminal:

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%

I don't understand, so I cannot make arbitrary names of examples? Do the names have to match function names of my package or of my file? This is in my test package, so I am probably trying to test function in a different package and file. How do I do this?

答案1

得分: 4

godoc对名称非常挑剔,你的Example函数名字需要与实际的函数名、类型名或其他相关。请参考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

你会注意到,Example被称为ExampleMultiplexer_simpleExampleMultiplexer_readWriter。这是因为我想要两个示例,我本可以只使用ExampleMultiplexer。这样做是因为我有一个叫做(确切地说)Multiplexer的类型。如果你有一个函数,你也可以这样做,但是名字必须匹配。

当使用后缀时,后缀的首字母必须是小写,否则godoc会将其丢弃。

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

英文:

godoc is very picky regarding the names, your Example function name needs to correlate with an actual function name, type name or other. See http://golang.org/pkg/testing/#hdr-Examples for reference.

Example of godoc exmaple: https://godoc.org/github.com/creack/multio#example-Multiplexer--ReadWriter, code: https://github.com/creack/multio/blob/master/example_test.go

You will notice, that the Example are called ExampleMultiplexer_simple ExampleMultiplexer_readWriter. This is because I wanted two example, I could have simply use ExampleMultiplexer. This works because I have a type called (exactly) Multiplexer. If you have a function, you can do the same, but the name needs to match.

When using a suffixe, it is very important that the suffix begins with a lowercase, otherwise godoc will discard it.

So to answer your question: yes you can use arbitrary name, but you still need to follow the godoc style of ExampleTypeName_suffix (lower case suffix first letter)

huangapple
  • 本文由 发表于 2014年7月2日 05:13:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/24519347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定