Created
May 29, 2019 15:21
-
-
Save jfmlima/5686bf59ee219e19c7fd1452fbaf3d8d to your computer and use it in GitHub Desktop.
Proper interfacing in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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