Skip to content

Instantly share code, notes, and snippets.

@meson10
Created December 14, 2014 15:14
Show Gist options
  • Save meson10/9d56daefe54c9c0739e9 to your computer and use it in GitHub Desktop.
Save meson10/9d56daefe54c9c0739e9 to your computer and use it in GitHub Desktop.
Golang Interfaces
package main
import "log"
type EventLogger interface {
Log() int
}
type EventA struct {
Id int
Description string
}
func (e EventA) Log() int {
log.Println(e.Id, e.Description)
return e.Id
}
type EventB struct {
Id int
Description string
Location string
}
func (e EventB) Log() int {
log.Println(e.Id, e.Description, "at", e.Location)
return e.Id
}
func logEvent(e EventLogger) {
e.Log()
}
func main() {
e1 := EventA{123, "Event A without location"}
e2 := EventB{123, "Event B with location", "Pune"}
e1.Log()
logEvent(e1)
logEvent(e2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment