TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Executable Examples in Go

228 pointsby gus_leonelalmost 2 years ago

21 comments

multanialmost 2 years ago
Python also has doctest <a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;doctest.html" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;doctest.html</a> since ... a long time now.<p>The documentation tools are not as advanced as Go&#x27;s and as far as I know, there&#x27;s no way to &quot;run the examples&quot; yourself.<p>It&#x27;s a great way to write tests for some functions though, to me the sweet spot is for small, without side effects functions. It&#x27;s totally possible for bigger functions but then all the initialization code and assertions can easily get in the way and make the documentation not super clear.
评论 #36272186 未加载
评论 #36274949 未加载
评论 #36274237 未加载
评论 #36271873 未加载
szopaalmost 2 years ago
Ok, this is indeed a very well kept secret. I have been programming go for a really long time, and was even a readability reviewer at Google, and I had no idea. Thank you, OP.
mabboalmost 2 years ago
Elixir has a similar concept with doc tests:<p><a href="https:&#x2F;&#x2F;elixir-lang.org&#x2F;getting-started&#x2F;mix-otp&#x2F;docs-tests-and-with.html" rel="nofollow">https:&#x2F;&#x2F;elixir-lang.org&#x2F;getting-started&#x2F;mix-otp&#x2F;docs-tests-a...</a><p>You&#x27;re comments&#x2F;docs for the method can have demonstration code that is executed when you run your tests, ensuring your docs remain accurate.<p>It&#x27;s a great pattern.
评论 #36272092 未加载
评论 #36271504 未加载
评论 #36274205 未加载
ilytalmost 2 years ago
I wondered why the hell people find that as a surprise here and figured out that is because the syntax for examples is not documented in [1] how to write a documentation part but instead in [2] &quot;how our testing framework works&quot;<p>- [1] <a href="https:&#x2F;&#x2F;go.dev&#x2F;blog&#x2F;godoc" rel="nofollow">https:&#x2F;&#x2F;go.dev&#x2F;blog&#x2F;godoc</a><p>- [2] <a href="https:&#x2F;&#x2F;pkg.go.dev&#x2F;testing#hdr-Examples" rel="nofollow">https:&#x2F;&#x2F;pkg.go.dev&#x2F;testing#hdr-Examples</a>
Waterluvianalmost 2 years ago
Doesn’t Rust have something like this? Where you can write code in comments and the code is actually tested?
评论 #36271824 未加载
评论 #36271586 未加载
评论 #36272522 未加载
评论 #36271571 未加载
peter_l_downsalmost 2 years ago
If it&#x27;s a secret, it&#x27;s only because the godoc format is so strangely obtuse about some things. It&#x27;s sort of like markdown, but sort of not. You can do lists, kind of. You can do links, kind of. What a pain!
WalterBrightalmost 2 years ago
D&#x27;s had that for many years. It&#x27;s not a secret!<p><a href="https:&#x2F;&#x2F;dlang.org&#x2F;phobos&#x2F;std_algorithm_searching.html#.any" rel="nofollow">https:&#x2F;&#x2F;dlang.org&#x2F;phobos&#x2F;std_algorithm_searching.html#.any</a><p>See the buttons below the example code. You can even edit the examples and try variations.
评论 #36272632 未加载
评论 #36273814 未加载
jamietannaalmost 2 years ago
Note that without the `Output:` line you can silently not be running these tests - <a href="https:&#x2F;&#x2F;github.com&#x2F;golangci&#x2F;golangci-lint&#x2F;issues&#x2F;3084">https:&#x2F;&#x2F;github.com&#x2F;golangci&#x2F;golangci-lint&#x2F;issues&#x2F;3084</a> covers when this can be problematic and there&#x27;s now a golangci-lint integrated linter for catching it
o1y32almost 2 years ago
I don&#x27;t understand why this is an article and why this shows up on hacker news at all. Other languages have been doing this for a long time as well. And there is no &quot;secret&quot; &quot;kept&quot; by anyone.
评论 #36273333 未加载
davelondonalmost 2 years ago
Check out my <a href="https:&#x2F;&#x2F;github.com&#x2F;dave&#x2F;rebecca">https:&#x2F;&#x2F;github.com&#x2F;dave&#x2F;rebecca</a> package for a neat way of building readme docs by embedding Go docs and examples.<p>It generates the big readme for <a href="https:&#x2F;&#x2F;github.com&#x2F;dave&#x2F;jennifer">https:&#x2F;&#x2F;github.com&#x2F;dave&#x2F;jennifer</a> (see <a href="https:&#x2F;&#x2F;github.com&#x2F;dave&#x2F;jennifer&#x2F;blob&#x2F;master&#x2F;README.md.tpl">https:&#x2F;&#x2F;github.com&#x2F;dave&#x2F;jennifer&#x2F;blob&#x2F;master&#x2F;README.md.tpl</a>).
评论 #36275258 未加载
mejutocoalmost 2 years ago
Adding to the list of languages that also have this: python. Unfortunately searching on google &quot;python executable comments&quot; only returns lobotomized results about what comments in python are.
评论 #36271686 未加载
评论 #36271826 未加载
starttoasteralmost 2 years ago
Neat! I’ve seen these in many high profile package’s GoDocs but never knew how to make them myself.
awinter-pyalmost 2 years ago
&gt; We’ve probably all had the unpleasant experience of copying and pasting an example code snippet from someone’s documentation, and finding not only that it doesn’t behave the way we expected, but that it doesn’t even compile. Not a good look for the project.<p>middle ground here is linting the examples if you can&#x27;t make them interactive, but yes this is powerful<p>forever shocked by huge oss packages with seemingly lots of contributors and stars where the docs are simply wrong<p>corollary: if your framework cannot compile a working program from a single small file (looking at you xcode), this kind of &#x27;here&#x27;s a working example&#x27; documentation becomes much harder
lloydatkinsonalmost 2 years ago
Pretty common for languages and frameworks to have this. Why is this being advertised as go only?
joluxalmost 2 years ago
Rust and Elixir do this too.
otikikalmost 2 years ago
I am 75% sure Rust has a similar thing
jghnalmost 2 years ago
R has had this since at least 2000. The man pages for each function contain an &quot;examples&quot; block, and these are expected (but don&#x27;t need) to be executable.<p>They then took it a step further a couple of years later going down the literate programming path with SWeave, and later Rmd.
Pbhaskalalmost 2 years ago
The book &quot;the go programming language&quot; has explained it too.
Patrickmialmost 2 years ago
tbh am not really really surprised but the fact that using Go and this not coming in mind is really surprising
jpozalmost 2 years ago
Great write up
arp242almost 2 years ago
It&#x27;s not a secret; they&#x27;re all over the place.<p>The article title is &quot;Executable examples in Go&quot;. You shouldn&#x27;t editorialize titles, especially when it makes the title significantly worse.<p><i>Edit: seems the article title has changed or is A&#x2F;B tested so never mind I guess (can&#x27;t delete since there are replies), but it&#x27;s still not a &quot;secret&quot;.</i>
评论 #36271909 未加载
评论 #36271339 未加载
评论 #36271388 未加载