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: should print package imports used in external argument and return types #22069
Comments
\cc @robpike to decide whether to add this feature. |
Two possible options:
|
This could make the output a lot messier, which would be counter to the idea behind the command, which is a quick and easy way to answer simple API questions. I never think about this problem anyway. Goimports takes care of it for me. I'll let others make comments. The idea of doing this only for "go doc " isn't right, as it sums all the imports any function/method uses, but any individual one may use none of them. |
I like the option of printing it with the package only. It keeps the individual declaration doc small.
Isn't "which type/package is this?" a simple API question?
Doesn't goimports only work in that case if you've already go-gotten the package and there's no other "closer" matching package that goimports could resolve the name to? In any case, this assumes the situation is that you're in an editor trying to call a function, whereas
I think this would add feature parity with godoc.org, which links package qualifiers to their documentation automatically. You can get information from godoc.org that you can't get with
I'm not sure I follow. In my example above, the only imports shown for the func are the ones that it exposes, not all the imports exposed by the entire package. |
Go doc tells you which package it is. It doesn't at the moment tell you all the packages you must import for dependencies, though. That's the question at hand. To your last point, I was referring to @dsnet's point about "full listings", that is, typing
with no arguments. |
(I can't tell which point this replies to. Can you please clarify?) Which package do you mean? If you mean the package containing the thing you're reading the documentation for, then sure, because you told it the package, but that's not the issue. If you mean the package of the imported type exposed by the thing you're reading the documentation for, then I don't think it does, as my example above showed:
It seems to me that exposed imports are a part of a package's exported interface, so understanding them is necessary to understanding the interface, so they should be part of the package's documentation. They are already part of the package's documentation for godoc.org in the form of hyperlinks, but they can't be represented as hyperlinks in the terminal, so |
Another option could be showing the import blocks as collapsed with a "...", like const/var blocks are:
For example, the
So you'd do a
|
It occurred to me that it's possible for the same package alias to refer to different packages across multiple files. If you only showed the imports at the package scope, there might not be one alias you could pick for an imported package that would work for the whole package. Basically, godoc.org is presenting the imports per file/declaration as URLs. So the equivalent in the terminal would be printing that import info with each declaration: import (
"context"
"google.golang.org/grpc/metadata"
)
func SendHeader(ctx context.Context, md metadata.MD) error
SendHeader sends header metadata. It may be called at most once. The
provided md and headers set by SetHeader() will be sent. The alternative is to normalize the aliases in the tool so they work across all the package declarations, but then the doc might not match the source, and you'd have to figure out how to always pick a good alias. |
@willfaught, does 'go doc metadata.MD' fail for you? It should find the right package. There's a tension in 'go doc' between having short concise output and being completely precise. For links and such it seems like the godoc web server would be a better starting point anyway. Provided 'go doc metadata.MD' works I think we should leave this alone. |
It depends:
But:
Also, strangely:
(Apparently, if it's a "root" package, it matches only on package name and not also declaration name. Bug?)
Adding an Would adding a flag to show the import info be an option? There's a flag to show unexported declarations.
My point, though, is that FWIW, I like that |
But if you add a line that's noise 99% of the time, then that makes it worse, not better. I could see making 'go doc grpc.metadata' work, perhaps. But nothing that changes the default output. |
Why would it be noise? In the simplest scenario, like for "strings", no imports are exposed, so there wouldn't be a line at all. If there are exposed imports, then you need all that info to fully understand the package's declarations. There's no useless info in those scenarios. Do you mean noise in the sense that you might only be interested in declarations that don't expose imports? If so, how is that noisier than listing all constant blocks when all I want to see is the functions?
If "grpc.metadata" were the name of a directory in $GOPATH/src, it would clash. I see what you mean, though. Maybe use the ... wildcard like ".../grpc/metadata"? But this assumes you know what the parent directory of the metadata package is. If you know that, then you probably already know which package metadata is. |
I understand your point but I don't see a good way to add this to the command-line version. For complete info like this the suggested source is a godoc web server (either locally or at godoc.org). |
So you can see which import paths the qualifiers map to. Without this info, documentation sources like godoc.org are more useful than
go doc
in these situations.What did you do?
$ go doc google.golang.org/grpc SendHeader
What did you expect to see?
Something like:
...so you can tell what the
metadata
inmd metadata.MD
(or evencontext
) is. Maybe you can get fancy and omit the explicit import alias if the package name matches the import path's base name. It's just a rough sketch of the idea.What did you see instead?
What's
metadata
? No idea, given just this info. The full package doc doesn't have the info either.System details
The text was updated successfully, but these errors were encountered: