Created
December 14, 2014 15:14
-
-
Save meson10/9d56daefe54c9c0739e9 to your computer and use it in GitHub Desktop.
Golang Interfaces
This file contains 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" | |
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