1

Is it possible to use go doc to view all sub-packages defined under a specific package?

Say, I want to view all sub-packages under crypto.

go doc crypto only lists what crypto defines, but no information about its sub-packages, like crypto/aes and crypto/cipher:

go doc crypto
package crypto // import "crypto"

Package crypto collects common cryptographic constants.

func RegisterHash(h Hash, f func() hash.Hash)
type Decrypter interface{ ... }
type DecrypterOpts interface{}
type Hash uint
    const MD4 Hash = 1 + iota ...
...
Shuzheng
  • 11,288
  • 20
  • 88
  • 186
  • I'm not sure about `go doc` but `go list` can do something like `go list package/prefix/...` — note the triple-dot. – kostix Dec 07 '19 at 17:09
  • 1
    See [How to list installed go packages](https://stackoverflow.com/questions/28166249/how-to-list-installed-go-packages/28166550#28166550); also [What's the Go (mod) equivalent of npm-outdated?](https://stackoverflow.com/questions/55866604/whats-the-go-mod-equivalent-of-npm-outdated/55866702#55866702) – icza Dec 07 '19 at 17:15
  • 2
    There is no concept of "under a package" or "sub-packages" in Go, so the question doesn't really make sense. – Jonathan Hall Dec 07 '19 at 17:15
  • @Flimzy - how would you refer to a “sub-package” then? – Shuzheng Dec 08 '19 at 08:57
  • I don't think you understood my comment. You don't refer to a sub-package, because there are no sub-packages. – Jonathan Hall Dec 08 '19 at 09:52

1 Answers1

2

If you want to see all sub-packages under a specific package you can use go list command:

go list crypto/...
crypto
crypto/aes
crypto/cipher
crypto/des
crypto/dsa
crypto/ecdsa
crypto/ed25519
crypto/ed25519/internal/edwards25519
crypto/elliptic
crypto/hmac
crypto/internal/randutil
crypto/internal/subtle
crypto/md5
crypto/rand
crypto/rc4
crypto/rsa
crypto/sha1
crypto/sha256
crypto/sha512
crypto/subtle
crypto/tls
crypto/x509
crypto/x509/pkix

Finally, for each package you can get the doc with go doc command.

go doc crypto/x509
...

You can write a script if you need to iterate over the results returned by go list.

Honestly, I think that the best way to consume the doc of std library is the Go website: https://golang.org/pkg/.

You can also start a local godoc web server to read the doc of your Go code:

godoc -http=:6060

*open your browser and visit localhost:6060*
Giulio Micheloni
  • 1,290
  • 11
  • 25
  • Thank you for an exercise answer. Is it true that Go has no concept of a sub-package? How does `go list crypto/...` works? Does it looks for a package called “crypto” in the `GOPATH` workspace, by searching the whole directory hierarchy, and then list all sub-packages under that package? – Shuzheng Dec 08 '19 at 09:01
  • About `go list` you can find its doc [here](https://golang.org/cmd/go/#hdr-List_packages_or_modules). [Go spec](https://golang.org/ref/spec#Packages) does not define sub-packages. In fact, every collection of files sharing the same package name belong to one package. Implementation may require that these files reside under the same directory. Given that subdirectories do exist, one may argue that also sub-packages exist. The _de facto_ standard is that sub-packages contain low-level API whereas higher level package provide more abstract API and interfaces. `crypto` package is an example. – Giulio Micheloni Dec 08 '19 at 09:48
  • "If you want to see all sub-packages" -- There are no sub-packages in Go. It would be better to re-phrase this to make sense. – Jonathan Hall Dec 08 '19 at 09:53
  • @Flimzy, thanks for the downvote. IMHO, this is just a terminology detail and even if go spec does not exactly report the wording "sub-package" it does not mean that gophers cannot use this word. It conveniently refer to packages contained in one package's subdirectories. – Giulio Micheloni Dec 08 '19 at 10:06
  • It's not just terminology. It's part of the Go spec. A sub-package implies a relationship between packages. There is no such relationship between Go packages. The _only_ namespace-defined relationship between Go packages relates to the use of an `internal` package. – Jonathan Hall Dec 08 '19 at 10:10
  • Of course people can use incorrect terms if they wish, but it's irresponsible and incorrect to encourage such misuses. – Jonathan Hall Dec 08 '19 at 10:11
  • Besides the fact that there is an actual relationship between one package and its "sub-package", which is that the latter resides in former's subdirectories. You will find many results on Google about "sub-packages". Apparently many people find it convenient to use this word. As long as the word conveys the message meant by source to destination, there is no harm in using it and is not irresponsible. – Giulio Micheloni Dec 08 '19 at 10:23
  • Again, many people use wrong terminology all the time. That's not a reason to propagate bad habits. – Jonathan Hall Dec 08 '19 at 13:48
  • I agree. However, in this context I don't see wrong terminology. I only see one word that is not explicitly reported in Go spec. – Giulio Micheloni Dec 08 '19 at 14:20