Skip to content

Instantly share code, notes, and snippets.

View mkock's full-sized avatar
💭
Currently building stuff in Go

Martin Kock mkock

💭
Currently building stuff in Go
View GitHub Profile
type calculator interface {
add(a, b int) int
sub(a, b int) int
div(a, b int) int
mul(a, b int) int
}
type mockCalculator struct {
calculator // Embeds the calculator interface
}
func main() {
p := player{} // Equivalent to p := player{nil}
fmt.Println(p.intelligence()) // panic: runtime error: invalid memory address or nil pointer dereference
}
type persona interface {
strength() int
intelligence() int
stamina() int
}
type dwarf struct {}
func (d *dwarf) strength() int { return 80 }
func (d *dwarf) intelligence() int { return 55 }
func (d *dwarf) stamina() int { return 90 }
type movie string
func (m movie) Movie() string { return string(m) }
type movieStar string
func (m movieStar) Star() string { return string(m) }
func movieOrStar(m interface{}) string {
switch v := m.(type) {
type car struct {}
func (c *car) start() { return }
func (c *car) stop() { return }
func (c *car) recycle() { return }
func (c *car) convertToBatMobile() { fmt.Println("I am now a BatMobile!") }
type vehicle interface {
start()
stop()
type car struct {}
func (c *car) start() { return }
func (c *car) stop() { return }
func (c *car) recycle() { fmt.Println("Recycling...") }
type vehicle interface {
start()
stop()
}
type car struct {}
func (c *car) start() { return }
func (c *car) stop() { return }
type vehicle interface {
start()
stop()
}
type equippable interface{
equip()
unequip()
}
type meleeWeapon interface {
equippable // Embeds equippable
slash()
}
type sword struct {}
func (s *sword) status() string {
return "Sword is damaged"
}
type player struct {
// unexported fields
}
func (p *player) status() string {
return "Player is eating"
}
func (p *player) sleep() {
// implementation goes here