-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/doc: add example support #26715
Comments
CC @robpike |
Punting to 1.13. |
I've recently started working on implementing this. I'm wonder whether gating the example output behind the On the other hand, examples can be quite long, particularly whole file examples demonstrating a package (e.g. see the examples of One option could be to always show examples for specific symbols which have been requested, but not for the whole package unless Anyone have any opinions as to what would be best. If not I'll probably proceed with examples only shown when |
Change https://go.dev/cl/445116 mentions this issue: |
I think that examples should not be shown without the |
Thanks for your thoughts @DeedleFake, very helpful to hear. I implemented a modified version of your suggestion in the latest change I submitted for review, based on feedback from Rob Pike. Take a look there if you're interested. |
Turned into a proposal to sort out the UI, which has not been settled satisfactorily through the CL. |
There has been significant discussion in the CL about how examples would appear in With no -ex flag present, should the command say when examples exist? You could put a line of commentary after the type/function/method saying there are examples, but that will get repetitive and unhelpful. The original draft was to suggest the command to get them, but that is also repetitive, repeating most of the information every time, and possibly many times in a given invocation. You could also annotate each type with the functions that exist. That has the advantage of similarity with the rest of the output, but it too has issues. Consider
There are many examples for this type:
The first two are bound to the type, so you might get
(I indented them for clarity, and maybe that's what should happen.) But what about all the others? They really gum up the output, with every method getting an example,
And in fairness, the boilerplate isn't adding much. Maybe it would be better to just annotate the default listing, like this:
with some suitable symbol for All this is just one line of inquiry. There is another option, which is to say nothing about examples by default. (I probably prefer that but am far from certain.) Having an The 'go doc' command excludes the tests, always. What if it sometimes didn't? Then you could ask,
for instance. Or even,
and similarly for benchmarks. That possibility should be factored into the way examples (and maybe tests and benchmarks) should be displayed. There is too much undesigned here to jump to a CL. |
This proposal has been added to the active column of the proposals project |
What if we show examples when you are looking for docs on a specific function, and otherwise never show them? Usually the go doc for a single function is pretty short so there’s room to fill and since it’s more targeted the examples are much more likely to be helpful |
This doesn't take into account package level examples, which do exist. Also, just to point out that while it is often true that function level documentation is short, that's not always the case - take time.Parse for example, which prints 48 lines in total, and the associated example for it is 51 lines (including output). This is unusual, but is not unprecedented. That said, I do personally agree with you that always printing examples when doing |
I definitely think that some pointer to there being executable examples available is important. I don't want to run I've been using the version I wrote for the CL day-to-day over the past week, where the text "There are executable examples for " is added to the end of the documentation, which I've found to be perfectly good, and not too annoyingly repetitive.
I don't mind there not being hints that particular functions or types have examples until the precise one is looked up. I think there's no neat way to do this; every option "gums up" things too much, even just annotating the listing like you gave examples for.
Interesting idea, but I think it's a bit less clear & discoverable than showing the example(s) associated with a regular identifier, particularly given that there can be several examples for an identifier. Consider Overall I prefer something close to what's currently in the CL. But I am a little biased (as well as inexperienced), as it's my first contribution to Go, so take what I suggest with an appropriate grain of salt. That said, I chose this issue to start on as I always hated that there was no way to get examples from the command line, and I use |
It's unclear how to annotate the lines in an unobtrusive manner. The best we came up with in a discussion is:
but that's still pretty obtrusive. If instead we require -ex to start telling about examples, then it would make sense for go doc -ex bytes.Buffer to say:
and then The downside is you'd need to know about and use |
How about this for the way to add example support? (1) The new flag -ex adds examples to the output, with examples attached to the API in the type, like
Note that (not shown) methods or other things with no examples still get printed too, showing the full API and that they have no examples. (2) If you say |
What about the probably rare but technically possible case where a regular function that's not in a |
@rsc So with no -ex flag, the command behaves as before? I like that, but one of the desires was to find a way to express the existence of examples in that case. However, I am leaning towards thinking that sounds good in theory but won't work well in practice. |
@robpike, yes, exactly. @DeedleFake you're right, that can happen, even the conflict if the second one is in a package foo_test file. Does anyone object to the proposal from last week? #26715 (comment) |
Sorry for taking so long to look at and respond to this. I like the above proposal, @rsc. To be clear, then, Your example output seems slightly incorrect to me, as ExampleBuffer() and ExampleBuffer_reader() are examples of Buffer, not NewBuffer(), so it would look more like this:
The lack of indentation to mark out these symbol-level examples is not ideal, as it doesn't clearly differentiate them as not part of the package API, but then starting the function list with indented Example functions would look odd too (though it would be in line with the preceding descriptive text). I'm unsure on which option would be preferable. |
@nickjwhite, yes, -ex only controls printing the examples in the usual list. |
Based on the discussion above, this proposal seems like a likely accept. |
No change in consensus, so accepted. 🎉 |
A question. Does -all also imply -ex? |
To go along with #25443, #25595, and #18807, it would be nice to get support for
godoc
's-ex
flag ingo doc
.The text was updated successfully, but these errors were encountered: