Created
March 28, 2017 08:53
-
-
Save ValerioSevilla/684712186502352f2500de20e65a74df to your computer and use it in GitHub Desktop.
Simple code demonstrating static binding in Go
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
package main | |
import "fmt" | |
type Thing interface { | |
coolify() | |
coolifyAndShout() | |
} | |
type CoolThing struct { | |
} | |
func (ct CoolThing) coolify() { | |
fmt.Println("Now I am cool") | |
} | |
func (ct CoolThing) coolifyAndShout() { | |
ct.coolify() | |
fmt.Println("YAAAAYYY") | |
} | |
type CoolerThing struct { | |
CoolThing | |
} | |
func (ct CoolerThing) coolify() { | |
ct.CoolThing.coolify() | |
fmt.Println("And even more") | |
} | |
func main() { | |
var t Thing | |
t = CoolerThing{} | |
t.coolify() // Calling CoolerThing's coolify | |
fmt.Println("\n\n") | |
t.coolifyAndShout() // This method is calling CoolThing's coolify, despite of t being a CoolerThing | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can play with this snippet in the Go Playground.