News for hackers. Stuff that matters.
Re:Interfaces in Go make it hard to navigate code by buro9 on Monday May 27, 2013 @05:39AM (#5774046)
This was one of those problems I had in the first month of using Go... along with import dependency cycles (as a consequence of code organisation), and not understanding the idiomatic approach to some things and writing completely unnecessary tracts of code.

Now that I have unlearned some of the approaches from other languages I actually feel this is a blessing. To oversimplify it: Anything that shares the same signature can pretty much be called as if it were that thing.

This gives the illusion of some fairly dynamic code (being able to assign some function or interface at runtime within compiled code) and allows for extremely low-coupling and for radical re-factoring to be extraordinarily simple to achieve.

Now that the penny has dropped I no longer find this as mysterious and worrying as the first few times I encountered it (and io.Reader was the most frequent instance of encountering it).

All that said... if the tooling (GoSublime via SublimeText2 in my case) were to show what matched these interfaces then that penny would have dropped a lot sooner.

Re:Interfaces in Go make it hard to navigate code by shurcooL on Monday May 27, 2013 @06:12AM (#5774101)
Can this information be automatically deduced by tools? If so, it's a great opportunity for tools to help out with understanding, while maintaing all the benefits of duck typing when the writing code.
Re:Interfaces in Go make it hard to navigate code by mseepgood on Monday May 27, 2013 @06:34AM (#5774150)
> Can this information be automatically deduced by tools?

Yes, it can and it should be fairly easy. The Go standard library already has a "go/ast" package. Would probably be similar to this documentation tool: http://code.google.com/p/rspace/source/browse/doc/doc.go?rep...

Re:Interfaces in Go make it hard to navigate code by jstanley on Monday May 27, 2013 @06:41AM (#5774162)
Given that it's automatically deduced by the compiler, I would hope so :)
Re:Interfaces in Go make it hard to navigate code by sirclueless on Monday May 27, 2013 @07:18AM (#5774218)
It's not that simple. I mean, given an os.File and a function that requires an io.Reader, it's pretty straightforward to discover that square peg goes in square hole. But just given either half of the relationship it's not currently so easy:

"This function needs an io.Reader, how would I get my hands on one of those?" Go doesn't currently have an easy way to tell you.

"This os.File has a bunch of stuff implemented. What can I do with it?" Go doesn't currently have an easy way to tell you.

The hard problem that remains to be solved is discovery. Someone somewhere needs to take a good long look at your project, the libraries available on your computer, and the standard library and connect the dots. It should be possible, most IDEs have this for other languages, when Go grows up you would expect these to be flagship features: "Find all known implementations of a given interface" and "Find all interfaces this type satisfies."

For example, you might eventually expect the locally generated versions of these two pieces of documentation to refer to each other, even though they currently do not:

http://golang.org/pkg/io/#Reader http://golang.org/pkg/os/#File

godoc already aggregates all your local packages' documentation into one place, adding cross-references to known implementations seems like an obvious incremental addition.

Re:Interfaces in Go make it hard to navigate code by jstanley on Monday May 27, 2013 @07:20AM (#5774226)
How many interfaces are there? You can iterate over all of them and see if the module implements them.

If you are doing this for many modules at a time you can do a lot better by caching signatures.