Skip to content

Instantly share code, notes, and snippets.

@jochasinga
Last active May 31, 2016 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jochasinga/6b4cf0e5f3476173ce0a476b8a6cceea to your computer and use it in GitHub Desktop.
Save jochasinga/6b4cf0e5f3476173ce0a476b8a6cceea to your computer and use it in GitHub Desktop.
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