Preemptive Interface Anti-Pattern in Go

What is a Preemptive Interface

type Auth interface {
GetUser() (User, error)
}
type authImpl struct {
// ...
}
func NewAuth() Auth {
return &authImpl
}

When preemptive interfaces are useful

// auth.java
public class Auth {
public boolean canAction() {
// ...
}
}
// logic.java
public class Logic {
public void takeAction(Auth a) {
// ...
}
}
// auth.java
public interface Auth {
public boolean canAction()
}
// authimpl.java
class AuthImpl implements Auth {
}
// logic.java
public class Logic {
public void takeAction(Auth a) {
// ...
}
}

Why this is not an issue in Go

// auth.go
type Auth struct {
// ...
}
// logic.go
func TakeAction(a *Auth) {
// ...
}
// logic.go
type LogicAuth interface {
CanAction() bool
}
func TakeAction(a LogicAuth) {
// ...
}

Unintended side effects of preemptive interfaces in Go

Best usage for interfaces in Go

--

--

--

Software Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jack Lindamood

Jack Lindamood

Software Engineer

More from Medium

Getting started with GO Programming Language — Part Two

Golang : A Hype or the Future?

Advanced gRPC (Deadlines, Cancellation, Error Handling, Multiplexing)

【Go】Implement SHA256, encryption and hashing in Go