3rd person examples for godoc?

polaris · · 583 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>IIRC godoc is able to recognize example functions from _test.go files, by defining <code>func Example_suffix()</code> functions. I would like to write examples from the 3rd person, e.g. how the code looks like when you include the package, but it seems impossible. I tried a tiny hack: including itself in the _test.go file:</p> <pre><code>package thing import ( &#34;testing&#34; thing &#34;github.com/tucnak/thing&#34; // itself ) func Example() { thing.Func() } </code></pre> <p>but it doesn&#39;t work (turns out recursive imports for tests don&#39;t work).</p> <hr/>**评论:**<br/><br/>: <pre><p>[deleted]</p></pre>dchapes: <pre><p>Also note, from <a href="https://golang.org/pkg/testing/#hdr-Examples" rel="nofollow">the documentation</a>:</p> <blockquote> <p>The entire test file is presented as the example when it contains a single example function, at least one other function, type, variable, or constant declaration, and no test or benchmark functions. </p> </blockquote></pre>tucnak: <pre><p>This won&#39;t let you use any unexported methods, which are often used in tests.</p></pre>: <pre><p>[deleted]</p></pre>tucnak: <pre><p>This won&#39;t compile (go test) either, since you&#39;d have two conflicting test packages in the same directory.</p></pre>: <pre><p>[deleted]</p></pre>tucnak: <pre><p>You are absolutely right, thanks!</p></pre>sh41: <pre><p>To do an external test package, you should add <code>_test</code> suffix to the package name. So try <code>package thing_test</code>.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

583 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传