Interfaces in Go make it hard to navigate code
swageroo.com
swageroo.com
I assure readers that this kind of problem is only faced by beginners. If the author approached the problem from the other direction, i.e. by asking "how do I extract a file's contents?", he would have looked up the documentation for os.File, and found it had a Read() method.
It's not often that you come across an arbitrary interface you have to implement, but aren't sure what to provide. If the documentation doesn't say, then presumably there's an type in the same package that you're meant to use. If not, domain-specific knowledge will help.
In this case it's necessary to have a basic idea of the way the standard library works - which as a beginner, the author doesn't have.
We rarely want a list of all possible types that can implement an interface. There are many different types that implement io.Reader in the standard library, it's not necessary for any particular package to be aware of all others ("slick"). And anyway, your editor might be able to help.
No, this problem is faced by anyone that needs to read code code written by other developers.
If you are faced by a code base written by a team of 30+ developers, good luck trying to find out which types implement which interface.
At least LiteIDE (http://code.google.com/p/liteide) is a possible help that eases the problem.
I can't help to think like what @tomp said. It's really up to the developers to annotate their code and document their Interfaces, specially when using a systems programming language like GO, comments are essential as the code is not as self-explanatory like Python or Ruby. GO provides an excellent tool to generate documentation from internal or external packages: http://golang.org/cmd/godoc/.
There's this great write-up on GO's Interface by Jordan Orelli: http://jordanorelli.tumblr.com/post/32665860244/how-to-use-i...
No, it's not a problem for code readers. If you see a file being passed as an argument to a function which expects a Reader, you know that it implements Reader. Otherwise it wouldn't have compiled.
Second, you can search for the function definition(s) that define the interface. Search for Write(p []byte) if you're looking for Writers.
If you're in control of the interface, you can just modify it slightly and see what breaks.
The thing about interfaces is that the function that takes the interface is the thing that defines how it is used. The implementation of the type that implements the interface is irrelevant.
log.Logger writes logging output to an io.Writer. It doesn't care what the implementation of any particular writer is... the writer could be sending data across the network, it could be writing it to the console, could be writing to a file... doesn't matter. Logger just calls Write() and merrily goes about its business. Trying to look up all the implementations of the io.Writer interface is not useful when looking at log.Logger. They're mostly unrelated.
Now, if you're trying to figure out how to do some specific thing, like log to a Windows named pipe, then you'd want to start looking around your named pipe library to see if it implements Write().... which it almost certainly does.
type Reader interface {
Read(p []byte) (n int, err error)
}
It should be obvious to any modestly experienced developer that an ordinary file object is likely to satisfy the requirement of having a common Read method.This seems like a basic failure to understand what a Go interface is, nevermind any understanding of the standard library.
For example if I have a Book class that implements a Read method (to mark the book as having been read, for example) then have I just created something which implements io.Reader?
Fair enough, I did say I was only speculating and I've learned something.
It's trying to merge the best parts of two very different worlds. A modest loss of safety is therefore tolerated.
I disagree. I've even brought this up before in passing on HN:
https://news.ycombinator.com/item?id=5639846
I've written many thousands of lines of Go code and have been using the language for more than a year and I still think the gist of the author's argument is a valid pain point when it comes to Go documentation. I'm now very familiar with the standard Go library, so the problem doesn't impact me as much as it used to, but I do find auto-generated Go documentation for new libraries I haven't yet used to be much harder to get a quick mental overview of than code autogenerated from other languages I've programmed in (including languages which are OO, functional or procedural).
Go is still my favorite language to program in, but I think there is a valid issue here (one that can be fixed with improved tooling)
I had a similar issue on my first toy program with Golang, but found it trivial to check the implemented methods, or assign it and let the compiler blow up if it doesn't fulfill the needs.
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.
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...
"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.
If you are doing this for many modules at a time you can do a lot better by caching signatures.
The author has avoided reading the "Learning Go" documents on the Go website which explains these differences from other languages http://golang.org/doc/ .
I'd like to argue against writing explicit implementation annotations in code unless really necessary. If the author had read the documents I mentioned earlier, he would have read this: http://golang.org/doc/effective_go.html#interface-names and realised that any value that implements io.Reader will have a function called Reader, no need for messy lists of code annotations describing lists of interface implementations. The authors complaint is somewhat like complaining that a car is not sold with the text "this goes into a garage" because otherwise they couldn't know- you'd expect the Car to implement the Volume interface the Garage takes so you could check.
To me, the way interfaces function is more intuitive. The author's problem is that they are solving the problem in reverse, it is strange to me that they would start the problem of reading a GIF from the end of 'decoding it' and not the end of 'reading it from something', the point of io.Reader is that it represents anything that can be read, whatever method he uses to read a GIF, be it out of a buffer, a connection, or a file will return a value that implements io.Reader if it is good code.
One reason I don't like dynamic languages for large scale applications, is that I have already seen how it works in the world of multi-site enterprise software with outsourcing partners around the world.
The type of companies where unit testing are seen as buzzwords from Silicon Valey startups and are only written if the customer explicitly requires them as part of the contract.
So I am yet to see any "large well-written dynamically typed code" out in the wild, at least in my area of work.
// File represents an open file.
// It implements the io.Reader, io.Writer and io.Closer interfaces.
type File struct {
...
}I'm brand new to Go. I read neither the os.File documentation, nor the io.Reader documentation, and I'm baffled why I couldn't figure out how to make a File into a Reader.
"How do I convert a Foo into a Bar" is a problem in all languages.
So if you need a Readable in Java ("Reader"[3] is an abstract base class, which incidentally lists all of its known subclasses, including FileReader if you follow through InputStreamReader), you have a ready listing available of all the common implementations, which solves this issue straight away, in a way that Go currently lacks.
There's nothing stopping Go from having this tooling too (with the caveat that interface cross-references would have to be "All known implemented interfaces" instead of "All implemented interfaces"), it's just not there yet.
[1]: http://docs.oracle.com/javase/7/docs/api/java/io/FileReader....
[2]: http://docs.oracle.com/javase/7/docs/api/java/lang/Readable....
[3]: http://docs.oracle.com/javase/7/docs/api/java/io/Reader.html
In general it would be neat if the "go" command supported some more advanced static analysis stuff beyond go fix, fmt, and vet.
A lot of this stuff could be done by reflect -- if only there was a repl!
// Read implements io.Reader
imho, an "implements" keyword would be a bad choice to describe what interface a type implements partly because it introduces dependencies between types which might not exist. also, you don't have to design this type-hierarchy from the very beginning. you may notice a particular pattern in a bunch of libraries after they are written, and noticing this you can describe an interface which captures it. and in some cases it might not even be possible to annotate this type information, as the source-code might even be available...
What a load of hand-wavey nonsense.
The lack of clearly defined interfaces in the documentation was one of the big turn offs when I tried go a while back.