Skip to content

Instantly share code, notes, and snippets.

@ValerioSevilla
Created March 28, 2017 08:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ValerioSevilla/684712186502352f2500de20e65a74df to your computer and use it in GitHub Desktop.
Save ValerioSevilla/684712186502352f2500de20e65a74df to your computer and use it in GitHub Desktop.
Simple code demonstrating static binding in Go
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
}
@ValerioSevilla
Copy link
Author

You can play with this snippet in the Go Playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment