Last active
May 31, 2016 12:39
-
-
Save jochasinga/6b4cf0e5f3476173ce0a476b8a6cceea to your computer and use it in GitHub Desktop.
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
package main | |
import "log" | |
// An interface for an abstraction between structs | |
type StringWriter interface { | |
Write(string) | |
} | |
// LogWriter now implements StringWriter | |
type LogWriter struct { | |
Counter int | |
} | |
func (w *LogWriter) Write(msg string) { | |
log.Println(msg) | |
w.Counter++ | |
} | |
// Watcher can rely on the abstraction rather than | |
// the implementation of LogWriter | |
type Watcher struct { | |
Writer StringWriter | |
} | |
// Then this method calls the StringWriter's method | |
// which can be mapped to just about any struct with | |
// any implementation of Write. | |
func (w *Watcher) Notify(msg string) { | |
w.Writer.Write(msg) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment