Skip to content

Instantly share code, notes, and snippets.

@jfmlima
Created May 29, 2019 15:21
Show Gist options
  • Select an option

  • Save jfmlima/5686bf59ee219e19c7fd1452fbaf3d8d to your computer and use it in GitHub Desktop.

Select an option

Save jfmlima/5686bf59ee219e19c7fd1452fbaf3d8d to your computer and use it in GitHub Desktop.
Proper interfacing in golang
func Test() {
http.HandleFunc("/", Potato{Conn: MockConnection{}}.potatoHandler)
}
type Connection struct{}
type MySQLConnection struct {
db *Connection
}
type MongoConnection struct {
db *Connection
}
type MockConnection struct {
}
func (m MySQLConnection) getCollection(query string) []string { return []string{"1"} }
func (m MongoConnection) getCollection(query string) []string { return []string{"1"} }
func (m MockConnection) getCollection(query string) []string { return []string{"1"} }
type DBConnection interface {
getCollection(string) []string
}
type Potato struct {
Conn DBConnection
}
func (p Potato) potatoHandler(w http.ResponseWriter, r *http.Request) {
p.Conn.getCollection("pairs")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment