8 ms·
Executable Examples in Go
- arp242 3y agoIt's not a secret; they're all over the place. The article title is "Executable examples in Go". You shouldn't editorialize titles, especially when it makes the title significantly worse. Edit: seems the article title has changed or is A/B tested so never mind I guess (can't delete since there are replies), but it's still not a "secret".
- mynameisvlad 3y agoAre we looking at the same article? Its title quite clearly is “Go’s best-kept secret: executable examples” so the title here is only minorly editorialized.
- jrockway 3y agoI also like how the HN submitter changed the name of the programming language to "Golang". I guess they also call Ruby "ruby-lang" since they didn't get "ruby.org"?
- mynameisvlad 3y agoGolang is a common way to refer to Go, especially for SEO purposes. “Go” is hard to search for. https://go.dev/doc/faq#go_or_golang https://go.dev/doc/faq#go_or_golang I use “golang” for clarity all the time, and I’m sure many others do as well. There’s no ambiguity you’re talking about the language then.
- arp242 3y agoI personally strongly prefer to use Go, and would never use "golang" or "Golang", but I don't mind if other people do and I'd never complain about it. The reason for this is simply that I don't like the sound of the word "golang"; there are some other words I avoid for similar reasons, I think everyone has a few of those. The thing is that the article title uses Go while the submission here uses Golang. I wouldn't be too happy if someone submitted one of my articles and changed words like that, especially if it's to something I dislike. That's also why I complained about the editorializing in the first place, because I'd be a bit peeved if someone added something like "best-kept secret" in there (but the article title changed, so that's now a moot point). Conversely, I'd also never change "golang" to "Go" when submitting something.
- Jtsummers 3y ago> I wouldn't be too happy if someone submitted one of my articles and changed words like that gus_leonel appears to be a pseudonym for the author of the post. They have essentially no comments and most of their submissions are related to this Bitfield Consulting. So it's the author editorializing their own submission titles.
- Waterluvian 3y agoI use Golang because Go is horrible to search for. I also get into a habit of “Rust lang” because of the video game. Yes, I could do a bunch of search engine customization stuff or pick someone else’s favourite engine. But this is simpler.
- mynameisvlad 3y agoBased on the submitter’s posting history and change in original article title, I’m going to go on a limb and say that the submitter is the original author. Yeah, the names don’t match, but out of all of the things they submitted, at least half are articles from this site. But beyond that, I mean, does it matter? You might not appreciate it, sure, but you’re also not the original author. I’d get the argument if the author came in and said it, but at this point this just seems like complaining for complaining’s sake and you not agreeing with the phrasing used in the article or submission title. That’s certainly your opinion to have, but is there any benefit to expressing it? This doesn’t seem like a relevant discussion about the article contents.
- arp242 3y ago> I mean, does it matter? You might not appreciate it, sure, but you’re also not the original author. Well, no, but I'd rather not assume anything: just let the author pick the title. (I assumed that "gus_leonel" wasn't the author, since John Arundel has his own HN account under his regular "bitfield" username, but they do post a lot from bitfieldconsulting, so idk).
- mynameisvlad 3y agoIf the original author has a problem with it, they are free to come in and make the complaint themselves. It’s your opinion to have but not your argument to make and while you may not appreciate it, even if the author isn’t the submitter (I still very much think they are), they clearly did considering the article title was changed to more closely match the submission, and they have a keyboard and can make the argument for themselves if they are irked by the changes. All this discussion seems to be doing is derailing the comment threads. Almost half the comments here are not about the article itself but your opinions on the words used in the title. This is also a funny way to not assume anything: > (I assumed that “gus_leonel” wasn’t the author > Well no, but I’d rather not assume anything I don’t see the benefit of this discussion existing in the first place, let alone continuing it, so I’m going to stop here.
- Jtsummers 3y agoThe article (at the linked page, not here) title was changed. The original was as GP described it, and in the last few minutes it was altered to match the HN title.
- arp242 3y agoIt shows like that for me too now, but I am 100% sure this wasn't the case a few minutes ago. I copy/pasted the title.
- mynameisvlad 3y ago[dead]
- HerrFolgreich 3y agoIn arp242's defense, I opened the article a couple of minutes ago and it showed the same title he was faced with until I refreshed the page.
- deleted 3y ago[deleted]
- jchw 3y agoIt seems as though the submitter is also the article author and changed it on the page to match the HN title. https://web.archive.org/web/20230117212610/https://bitfieldconsulting.com/golang/examples https://web.archive.org/web/20230117212610/https://bitfieldc... This is an interesting conundrum. Clearly the new title was chosen because it sounds more alluring; I mean, it hit the front page. But also, the OP could've easily done this prior to submitting and it would be totally normal, since people adjust article and post titles all the time. Does come off as mildly sneaky when you do it in this order, but maybe I'm just nitpicky.
- mperham 3y agohttps://xkcd.com/1053/ https://xkcd.com/1053/
- jerf 3y agoIt certainly isn't a secret in the sense that they didn't want you to know about this feature, but it is a de facto secret in that you can find people who have been programming in Go for year who don't realize they exist. In addition to the other replies you can already find in this HN discussion mentioning that, I can attest to the fact that I've personally introduced them to multi-year programmers in Go as well. I hypothesize that a very simple and easy-to-make mistake created this situation, and it is as simple as this: Godoc collapses the examples. Behind undistinguished links. Look at the current rendering for the documentation for net/http, the core HTTP package: https://pkg.go.dev/net/http https://pkg.go.dev/net/http Your challenge is to scroll through the package with the scrollbar, looking for the Examples. Of course, if enough people try this, some will find them quickly. Someone will simply jerk their mouse and scroll right to it in a quarter of a second, of course. But in general, they're not easy to find, even when I'm telling you to look for them. If you don't even know they are there they are super easy to miss. The main listing for the examples is between the Index and the Constants. In the left section breakdown, they don't get their own top-level section but are instead by default buried unexpanded below Index, where I would personally assert they don't belong. Even if you scroll to the documentation for a specific function that includes an example, such as Hijacker: https://pkg.go.dev/net/http#Hijacker https://pkg.go.dev/net/http#Hijacker , I think it's very easy to be scanning through and miss the little Example link in all the noise. And again, in this context I'm calling your attention to it; in normal circumstances I think link blindness kicks in and people can read these docs for years without catching them. And then of course even if you do catch them it's easy to come away thinking it must be something special the standard library can do but you can't, because that is a Go thing, and so few 3rd party packages avail themselves of this. But you can easily do them yourself. It's little more than a slightly specially-formatted test function.
- arp242 3y agoWell, it doesn't really matter – my main complaint was that the title was editorialized in a way that I personally wouldn't appreciate for my own article, but now the article title changed and the submitter may be the article author (not entirely clear). I would have deleted my previous post if I could.
- 3y ago
- mabbo 3y agoElixir has a similar concept with doc tests: https://elixir-lang.org/getting-started/mix-otp/docs-tests-and-with.html https://elixir-lang.org/getting-started/mix-otp/docs-tests-a... You're comments/docs for the method can have demonstration code that is executed when you run your tests, ensuring your docs remain accurate. It's a great pattern.
- xrd 3y agoI came here to say this and you beat me to it! Love this part of elixir. It's a good way to think about your functions: if you can't write a doc test, you've probably written a function with side effects.
- mattkrause 3y agoJulia (Documenter.jl), Python (doctest) and R have (doctest) them too.
- otherme123 3y agoNim: runnableExamples.
- nazka 3y agoIs it similar to what Rust does?
- davelondon 3y agoCheck out my https://github.com/dave/rebecca https://github.com/dave/rebecca package for a neat way of building readme docs by embedding Go docs and examples. It generates the big readme for https://github.com/dave/jennifer https://github.com/dave/jennifer (see https://github.com/dave/jennifer/blob/master/README.md.tpl https://github.com/dave/jennifer/blob/master/README.md.tpl).
- badrequest 3y agoDave I've been a heavy user of Jennifer for quite some time, thank you for making such an awesome library.
- peter_l_downs 3y agoIf it's a secret, it's only because the godoc format is so strangely obtuse about some things. It's sort of like markdown, but sort of not. You can do lists, kind of. You can do links, kind of. What a pain!
- jghn 3y agoR has had this since at least 2000. The man pages for each function contain an "examples" block, and these are expected (but don't need) to be executable. They then took it a step further a couple of years later going down the literate programming path with SWeave, and later Rmd.
- Waterluvian 3y agoDoesn’t Rust have something like this? Where you can write code in comments and the code is actually tested?
- nrub 3y agoYup
- andrewjf 3y agoYes, they’re called documentation tests. https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.html https://doc.rust-lang.org/rustdoc/write-documentation/docume...
- jerf 3y agoGo's contribution here is not so much inventing the idea... Python's had it since the 20th century, for instance... but accidentally hiding them in the docs so that so many people, even long-time Go programmers, don't realize Go has it too.
- the_gipsy 3y agoLightyears ahead.
- starttoaster 3y agoNeat! I’ve seen these in many high profile package’s GoDocs but never knew how to make them myself.
- mejutoco 3y agoAdding to the list of languages that also have this: python. Unfortunately searching on google "python executable comments" only returns lobotomized results about what comments in python are.
- szopa 3y agoOk, 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.
- multani 3y agoPython also has doctest https://docs.python.org/3/library/doctest.html https://docs.python.org/3/library/doctest.html since ... a long time now. The documentation tools are not as advanced as Go's and as far as I know, there's no way to "run the examples" yourself. It's a great way to write tests for some functions though, to me the sweet spot is for small, without side effects functions. It'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.
- tuukkah 3y agoI don't know why you'd need to run an example apart from when running tests. After all, the examples have been tested to output what the example says.
- multani 3y agoThat can be useful because the doc website turns into some kind of sandbox where you can tweak directly the test function and see the result without having to write the code yourself.
- roflchoppa 3y agoDude doc tests are the one thing I really really miss when I moved over from working on python backend to a typescript backend… Actually unittest as a whole, testing in ts/JS feels like a mess, everything is just fanned out everywhere, there has been such a lack of structure in our codebase. I’m sure there are examples of well organized testing suites, but I’ve yet to find them.
- capableweb 3y agoThing is, most testing frameworks/libraries in JS gives you a lot of freedom to structure the overall testing architecture however you want, without being much picky about it. That means teams writing the tests have to carefully consider their testing architecture, treating it like production code. But most people don't consider testing code "production" code, they treat it like something they should be able to write once and then kind of forget about it. So zero regard for architecture will lead to "everything is just fanned out everywhere" and "lack of structure", no matter what language you're using, not just JS. What you're talking about tells us more about your team's engineering practices than how messy the JS testing ecosystem is.
- awinter-py 3y ago> 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. middle ground here is linting the examples if you can't make them interactive, but yes this is powerful forever shocked by huge oss packages with seemingly lots of contributors and stars where the docs are simply wrong corollary: if your framework cannot compile a working program from a single small file (looking at you xcode), this kind of 'here's a working example' documentation becomes much harder
- WalterBright 3y agoD's had that for many years. It's not a secret! https://dlang.org/phobos/std_algorithm_searching.html#.any https://dlang.org/phobos/std_algorithm_searching.html#.any See the buttons below the example code. You can even edit the examples and try variations.
- jchw 3y agoI was curious for exactly how long "many years" was, and it appears it was added sometime between 2016 to 2017. The last archive without it is from 2016, the next archive with it is 2017: https://web.archive.org/web/20160405080736/https://dlang.org/phobos/std_algorithm_searching.html https://web.archive.org/web/20160405080736/https://dlang.org... Now that's of course just the website feature. Maybe D had executable examples that were tested by the compiler before Go, which would still be very interesting. However, it does appear Go at least did have the in-browser feature as far back as 2015: https://go.dev/blog/examples https://go.dev/blog/examples Not that this matters for much other than historical interest: good ideas like this should be copied shamelessly (and obviously it's not a terribly original idea to begin with, just a good implementation of it.)
- WalterBright 3y ago> Maybe D had executable examples that were tested by the compiler before Go Sounds like D's ability to specify unit tests in production source code, and have them optionally executed, fits the bill for that. https://dlang.org/spec/unittest.html https://dlang.org/spec/unittest.html Builtin unit tests were a great leap forward for code verification.
- jchw 3y agoHaving unit testing built-in to the programming language and tooling is a huge plus, absolutely. I don't know who originally came up with this, but I swear, it would be worth some recognition :) (And thankfully, whoever did originally come up with this idea, it has been copied a ton by the recent crop of programming language designs.)
- o1y32 3y agoI don'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 "secret" "kept" by anyone.
- deleted 3y ago[deleted]
- otikik 3y agoI am 75% sure Rust has a similar thing
- lloydatkinson 3y agoPretty common for languages and frameworks to have this. Why is this being advertised as go only?
- jolux 3y agoRust and Elixir do this too.
- jamietanna 3y agoNote that without the `Output:` line you can silently not be running these tests - https://github.com/golangci/golangci-lint/issues/3084 https://github.com/golangci/golangci-lint/issues/3084 covers when this can be problematic and there's now a golangci-lint integrated linter for catching it
- jpoz 3y agoGreat write up
- ilyt 3y agoI 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] "how our testing framework works" - [1] https://go.dev/blog/godoc https://go.dev/blog/godoc - [2] https://pkg.go.dev/testing#hdr-Examples https://pkg.go.dev/testing#hdr-Examples
- Patrickmi 3y agotbh am not really really surprised but the fact that using Go and this not coming in mind is really surprising
- Pbhaskal 3y agoThe book "the go programming language" has explained it too.