From f01e09337478f246b2b96d15a74c6283e81f09fe Mon Sep 17 00:00:00 2001 From: Steven Degutis Date: Fri, 16 Mar 2012 07:46:12 -0500 Subject: [PATCH] interfaces --- about_interfaces.go | 28 ++++++++++++++++++++++++++++ setup_koans_test.go | 4 ++-- 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 about_interfaces.go diff --git a/about_interfaces.go b/about_interfaces.go new file mode 100644 index 0000000..589caa5 --- /dev/null +++ b/about_interfaces.go @@ -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++ +} diff --git a/setup_koans_test.go b/setup_koans_test.go index 328c5af..5b1e977 100644 --- a/setup_koans_test.go +++ b/setup_koans_test.go @@ -25,10 +25,10 @@ func TestKoans(t *testing.T) { //testEnumeration() //testAnonymousFunctions() //testVariadicFunctions() - testFiles() + //testFiles() + testInterfaces() // TODO: ie, gameplan - //testInterfaces() //testMaps() //testStructs() //testAllocation()