interfaces

This commit is contained in:
Steven Degutis
2012-03-16 07:46:12 -05:00
parent 173307660d
commit f01e093374
2 changed files with 30 additions and 2 deletions

28
about_interfaces.go Normal file
View File

@@ -0,0 +1,28 @@
package go_koans
func testInterfaces() {
mspaint := &program{3} // mspaint is a kind of *program, which is a valid 'runner'
runOnce(mspaint) // runOnce takes an abstract 'runner' type
assert(mspaint.runTimes == __int__) // conformed interfaces need not be declared, they are inferred
}
// abstract interface and function that requires it
type runner interface {
run()
}
func runOnce(r runner) {
r.run()
}
// concrete type implementing the interface
type program struct {
runTimes int
}
func (self *program) run() {
self.runTimes++
}

View File

@@ -25,10 +25,10 @@ func TestKoans(t *testing.T) {
//testEnumeration()
//testAnonymousFunctions()
//testVariadicFunctions()
testFiles()
//testFiles()
testInterfaces()
// TODO: ie, gameplan
//testInterfaces()
//testMaps()
//testStructs()
//testAllocation()