r/golang•
Lydrin

Trying to understand Go interfaces as a newcomer

Hi Gophers !

I'm trying to learn Go on my spare time coming from Java / Node.js background and there is something I don't really understand about interfaces.

Let's say that I have an http.ResponseWriter :

type ResponseWriter interface {
    [...]
    Write([]byte) (int, error)
    [...]
}

And now let's consider the io.Writer interface :

type Writer interface {
    Write(p []byte) (n int, err error)
}

Put it this way, it's kind of obvious, http.ResponseWriter implements io.Writer, thus I can use something like this :

func Handler(w http.ResponseWriter, req *http.Request) {
    io.WriteString(w, "blabla.\n")
}

As a new Gopher, this is kind of ... odd in my opinion, since the interface implementation is not explicit in Go, it seems to me that you must know them by heart.

What if I didn't made the effort to read the Writer and ResponseWriter interface ? It's not really obvious that ResponseWriter implements Writer (besides the name).

It seems to me that it may become hard to understand which interface implements which in some complex scenarios

Why can't we write something like this ?

type ResponseWriter interface {
    [...]
    io.Writer
    [...]
}

This way, it becomes obvious that ResponseWriter is an io.Writer, and thus I can easily know that I can use all the function that takes an io.Writer interface as an argument.

I don't want to be disrepectful to the langage, and perhaps I'm just wrong, but I believe that the implicit implementation may cause trouble to understand code. For example, as a new Gopher it's sometimes hard to understand that I can use a function because my type implements its methods.

Not to say that Java is perfect, but it seems to me that having explicit interface implementation makes the code easier to understand.

Thank you, and please let me know if I made an English mistake !

Posted Sep 22, 2020, 9:59 PM UTC

06
peterbourgon•

Why can't we write something like this ?

You can! That's valid. It's also totally equivalent to the existing definition. I'm not entirely sure why it's not expressed that way.

—

I don't want to be disrepectful to the langage, and perhaps I'm just wrong, but I believe that the implicit implementation may cause trouble to understand code. For example, as a new Gopher it's sometimes hard to understand that I can use a function because my type implements its methods.

An important and fundamental thing about Go interfaces is that they're meant to be defined and used by the code that consumes them, as a way to express the behavior that they need from a dependency. This is different than many (most?) other languages, where things like interfaces are typically defined and used by the code that implements them, as a way to express the behavior that they're providing to consumers. This "decoupling" enables a lot of the flexibility and power of the language.

20
rodrigocfd•

Not to say that Java is perfect, but it seems to me that having explicit interface implementation makes the code easier to understand.

Different languages aren't only different syntaxes to write the same concepts. They have different concepts. Some concepts are common like if/else and for, but there's a whole world of crazy stuff out there.

Take C++ for example. It doesn't even have interfaces, just the so-called "pure abstract classes", which are classes with function signatures = 0. Wut?? Yes.

Interfaces in Go have a different meaning than Java. They're something like "I accept any object who bark()", instead of "specify everyone who bark()".

Anyway it gets clearer with the time. Just start a project and try not use Go to write Java: keep your mind open.

5
drvd•

What if I didn't made the effort to read the Writer and ResponseWriter interface ?

A very sensible recommendation for working with Go is: Do read the documentation! Really. Read the documentation of each function, type, method you use and read the whole package documentation of any package you use.

I know this sound very strange for people coming from other languages which seem to be used to a) documentation providing no value and b) producing code by autocompleting in the IDE.

Go's documentation is short and generally provides valuable and important information. Just add one more principle to you best practices (like DRY, SOLID, WET, GRASP, KISS): DOREGODO for DO REad GO DOcumentation (aka known as RTFM).

5
ajr901•

The best advice I can offer you is to not try to write Go with the mindset or experience you have from any other language. Writing Go like Java or like C# or like JavaScript, etc., is not going to do you much good and will likely get in your way.

Something that benefited me a lot was to let go of everything I thought I knew about programming and to re-learn it from the Go perspective. Stop questioning why Go does something the way it does and simply accept it as an equally valid way of doing things. Stop trying to apply previous knowledge of how to do X thing in Java, and accept that the Go way of doing it is simple different, but not necessarily better or worse; it just is.

3
aksdb•

The other way around can work quite well, though. I took a lot of simplicity from Go back to my Kotlin and Java code. Now I miss goroutines and channels as first class citizens and of course the extensive stdlib. But still, learning Go can massively help seeing problems with a different and simpler solution in mind.

1
funkiestj•

I don't want to be disrepectful to the langage, and perhaps I'm just wrong, but I believe that the implicit implementation may cause trouble to understand code

  1. All complex code is hard to read. Go is harder for you than Java because it is new, not because interfaces are implicit.
  2. you need better tools to help you read/learn code. Go guru works for me (jump to definition, find references, describe). One of these days the Go LSP will be production quality and that will be better.
1