Few months into learning Go, I just discover that os.File implements the io.Reader interface by implementing the Read(b []byte) (n int, err error) function. This allows me to us
https://github.com/dominikh/implements can do this:
implements is a command line tool that will tell you which types implement which interfaces, or which interfaces are implemented by which types.
e.g.
~ implements -types=crypto/cipher
crypto/cipher.StreamReader implements...
io.Reader
*crypto/cipher.StreamReader implements...
io.Reader
crypto/cipher.StreamWriter implements...
io.Closer
io.WriteCloser
io.Writer
*crypto/cipher.StreamWriter implements...
io.Closer
io.WriteCloser
io.Writer
For all you vim junkies out there, vim-go supports advance code analysis using the :GoImplements
, :GoCallees
, :GoChannelPeers
, :GoReferrers
etc. oracle commands.
For example, if I have a Calculator
interface and implementation that looks like:
type Arithmetic interface{
add(float64, float64) float64
}
type Calculator struct{}
func (c *calculator) add(o1, o2 float64) float64 {
// ... stuff
}
Then running :GoImplements
in vim with my cursor on the type Arithmetic interface
will yield something like:
calculator.go|8 col 6| interface type Arithmetic
calculator.go|3 col 6| is implemented by pointer type *calculator
Now if I moved my cursor to the type Calculator struct{}
line and run :GoImplements
, I will get something like:
calculator.go|3 col 6| pointer type *calculator
calculator.go|8 col 6| implements Arithmetic
Note: If you got an "unknown command" error, try execute :GoInstallBinaries
first before re-trying.
You can find the info you want and more using the godoc command's static analysis tools. Run the following at the command line: godoc -http=":8080" -analysis="type"
. Using the documentation you can find out what types implement an interface and the method set for a type.
There is also a pointer analysis that allows you to find callers and callees of various types. The channel send<--->receive analysis is pretty neat.
You can also read more about the static analysis done by the godoc tool at http://golang.org/lib/godoc/analysis/help.html