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
| 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 | |
| } |
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
| func main() { | |
| p := player{} // Equivalent to p := player{nil} | |
| fmt.Println(p.intelligence()) // panic: runtime error: invalid memory address or nil pointer dereference | |
| } |
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
| 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 } |
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
| 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) { |
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
| 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() |
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
| 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() | |
| } |
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
| type car struct {} | |
| func (c *car) start() { return } | |
| func (c *car) stop() { return } | |
| type vehicle interface { | |
| start() | |
| stop() | |
| } |
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
| type equippable interface{ | |
| equip() | |
| unequip() | |
| } | |
| type meleeWeapon interface { | |
| equippable // Embeds equippable | |
| slash() | |
| } |
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
| type sword struct {} | |
| func (s *sword) status() string { | |
| return "Sword is damaged" | |
| } |
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
| type player struct { | |
| // unexported fields | |
| } | |
| func (p *player) status() string { | |
| return "Player is eating" | |
| } | |
| func (p *player) sleep() { | |
| // implementation goes here |