Skip to content

Instantly share code, notes, and snippets.

@soareschen
Created March 28, 2015 12:11
Show Gist options
  • Save soareschen/8ebcc35035a6e19ad093 to your computer and use it in GitHub Desktop.
Save soareschen/8ebcc35035a6e19ad093 to your computer and use it in GitHub Desktop.
Prototypal Inheritance in Go
package main
import (
"fmt"
"strings"
)
type Base interface {
Foo() string
Bar() string
}
type BaseImpl struct {
foo string
bar string
}
type Proto struct {
Base
}
func (self *BaseImpl) Foo() string {
return fmt.Sprintf("BaseFoo: %s", self.foo)
}
func (self *BaseImpl) Bar() string {
return fmt.Sprintf("BaseBar: %s", self.bar)
}
func (p *Proto) Foo() string {
return strings.ToUpper(p.Base.Foo())
}
func main() {
var base Base = &BaseImpl{"MyFoo", "MyBar"}
var proto Base = &Proto{base}
fmt.Printf("foo: %s; bar: %s\n", proto.Foo(), proto.Bar())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment