Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Documentation On "All Known Implementation" of Interfaces

Tags:

go

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 use a buffered reader to read a file by do something like:

f, err := os.Open("myfile.txt")
bufReader := bufio.NewReader(f)

Unless I miss it, it looks like there isn't an "All Known Implementing Classes" in Go documents on interfaces, like those found in Java interfaces documentation.

Are there any ways to identify the types that implement an interface in Go?

like image 225
ivan.sim Avatar asked Aug 01 '15 06:08

ivan.sim


1 Answers

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.

like image 82
ivan.sim Avatar answered Sep 22 '22 23:09

ivan.sim