# I am the Watcher. I am your guide through this vast new twtiverse.
# 
# Usage:
#     https://watcher.sour.is/api/plain/users              View list of users and latest twt date.
#     https://watcher.sour.is/api/plain/twt                View all twts.
#     https://watcher.sour.is/api/plain/mentions?uri=:uri  View all mentions for uri.
#     https://watcher.sour.is/api/plain/conv/:hash         View all twts for a conversation subject.
# 
# Options:
#     uri     Filter to show a specific users twts.
#     offset  Start index for quey.
#     limit   Count of items to return (going back in time).
# 
# twt range = 1 5
# self = https://watcher.sour.is/conv/i4hjp4q
Go Wiki: Go Code Review Comments - The Go Programming Language 👈 Can Someone better at Go than me explain the guidance on interfaces here such that I understand why? Explain this to me like I'm five! Apparently this was talked about on Reddit some ~3yrs ago, but I missed the memo 📋 #Go #Inerfaces~
Go Wiki: Go Code Review Comments - The Go Programming Language 👈 Can Someone better at Go than me explain the guidance on interfaces here such that I understand why? Explain this to me like I'm five! Apparently this was talked about on Reddit some ~3yrs ago, but I missed the memo 📋 #Go #Inerfaces~
@prologic I had to read it two, three times, but I think I got it. Let me try. I believe most of it is general advice, not just specific to Go only.

# No Interface Is A Good Interface

First of all, don't start out with an interface right at the very beginning. Only create one if you later on come to the conclusion that you really have to, because chances for truly needing one are actually slim.

I experience that at my dayjob, too. There is a code base where I always wonder why certain interfaces exist in the first place. They're all implemented by exactly one type each, which is kind of useless. Just the type alone would totally suffice.

That train of thought to always also have an interface along with an implementing type might come from the Java enterprise world, at least that's where I encountered it really heavily. I never liked that. It just makes the code arbitrarily more complicated than it needs to be. The best code is the one that doesn't even exist. Simpler is better. Complexity is the root of all evil.

Advocates of the type with interface faction then like to argue: "But maybe sometime in the future we would like to create a second type that implements this interface, you will never know@11!! Or think of refactoring, we can also change the underlying implementation completely when we have an interface in front of it without people knowing!" But that basically never happens in reality. It reminds me a bit of premature optimization, preparing for the unknown future. Firstly, things turn out differently and secondly, other than one thinks. :-)

To be fair, thinking about what might happen or not is still a very valid thing. In my opinion it is even done not enough in this agile world. Implementation first, consideration second (if at all). But there are limits. So, start out simple. No interface for you at first.

That general rule goes at least for application development, it can be a little bit different when you write a library. More flexibility _might_ be actually helpful there.

# Interface Placement

When you define an actually beneficial interface, then place it in the same package where it is actually used in. Or lexically close to where is is used for that matter. As oposed to in the package where the implementing type resides in. This recommendation is very logical to me. The interface describes the API, so it should also go along with the rest of our API.

# Return Values

When you have a factory function to create a type that implements an interface, return that implementing type, not the interface. Using ugly suffixes in identifiers to help visualize the concept:

o
type FooInterface interface {
    Foo()
}

type FooImplementation struct { }
func (f *FooImplementation) Foo() { }

func NewFoo() *FooImplementation /* as opposed to FooInterface */ {
    return &FooImplementation{}
}


Most of the time I agree on that rule (it feels natural and correct), sometimes I don't. I reckon this depends on the exact use case at hand.

# Testing

When you have a type that you want to test, the recommendation is to not create dedicated interfaces for testing purposes only in order to mock something. If you do, this smells like a bad API design of the type in the first place. Instead, try to make its regular, productive API better, so it can be also used when testing the type.

Phew, this turned out to be a much longer post than I first anticipated. ;-) I hope this helps a bit.
@lyse Yeah, I guess it's not a hard and fast rule as such. I think you're right in that it probably comes from the Java enterprise world. This was sparked from when I decided (_for some reason_) in Bitcask v2 that I would define a set of interfaces for the library's public facing API. Turns out that probably not really needed or that useful I guess.
@lyse Yeah, I guess it's not a hard and fast rule as such. I think you're right in that it probably comes from the Java enterprise world. This was sparked from when I decided (_for some reason_) in Bitcask v2 that I would define a set of interfaces for the library's public facing API. Turns out that probably not really needed or that useful I guess.