interfaces
This commit is contained in:
28
about_interfaces.go
Normal file
28
about_interfaces.go
Normal 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++
|
||||||
|
}
|
||||||
@@ -25,10 +25,10 @@ func TestKoans(t *testing.T) {
|
|||||||
//testEnumeration()
|
//testEnumeration()
|
||||||
//testAnonymousFunctions()
|
//testAnonymousFunctions()
|
||||||
//testVariadicFunctions()
|
//testVariadicFunctions()
|
||||||
testFiles()
|
//testFiles()
|
||||||
|
testInterfaces()
|
||||||
|
|
||||||
// TODO: ie, gameplan
|
// TODO: ie, gameplan
|
||||||
//testInterfaces()
|
|
||||||
//testMaps()
|
//testMaps()
|
||||||
//testStructs()
|
//testStructs()
|
||||||
//testAllocation()
|
//testAllocation()
|
||||||
|
|||||||
Reference in New Issue
Block a user