Created
March 28, 2015 12:11
-
-
Save soareschen/8ebcc35035a6e19ad093 to your computer and use it in GitHub Desktop.
Prototypal Inheritance in Go
This file contains 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" | |
"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