From 49cd5e32478d7c95383e833ffd5b7856d12efa20 Mon Sep 17 00:00:00 2001 From: Steven Degutis Date: Sun, 11 Mar 2012 20:56:02 -0500 Subject: [PATCH] slices and arrays --- arrays.go | 6 ++++++ setup_koans_test.go | 3 ++- slices.go | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 slices.go diff --git a/arrays.go b/arrays.go index 132c137..e4d0996 100644 --- a/arrays.go +++ b/arrays.go @@ -11,6 +11,8 @@ func testArrays() { assert(len(fruits) == __int__) // the length is what the length is assert(cap(fruits) == __int__) // it can hold no more + assert(fruits == [4]string{}) // comparing arrays is not like comparing apples and oranges + tasty_fruits := fruits[1:3] // defining oneself as a variation of another assert(tasty_fruits[0] == __string__) // slices of arrays share some data assert(tasty_fruits[1] == __string__) // albeit slightly askewed @@ -24,4 +26,8 @@ func testArrays() { assert(fruits[1] == __string__) // how about the second? assert(fruits[2] == __string__) // surely one of these must have changed assert(fruits[3] == __string__) // but who can know these things + + veggies := [...]string{"carrot", "pea"} + + assert(len(veggies) == __int__) // array literals need not repeat an obvious length } diff --git a/setup_koans_test.go b/setup_koans_test.go index 7590b4d..24191e9 100644 --- a/setup_koans_test.go +++ b/setup_koans_test.go @@ -17,9 +17,10 @@ var __bool__ bool = false var __float32__ float32 = -1.0 func TestKoans(t *testing.T) { - testNumbers() + //testNumbers() //testStrings() //testArrays() + testSlices() fmt.Printf("\n%c[32;1mYou won life. Good job.\n\n", 27) } diff --git a/slices.go b/slices.go new file mode 100644 index 0000000..552b50b --- /dev/null +++ b/slices.go @@ -0,0 +1,18 @@ +package go_koans + +func testSlices() { + fruits := []string{"apple", "orange", "mango"} + + assert(fruits[0] == __string__) // slices seem like arrays + assert(len(fruits) == __int__) // in nearly all respects + + tasty_fruits := fruits[1:3] // we can even slice slices + assert(tasty_fruits[0] == __string__) // slices of slices also share some data + assert(tasty_fruits[1] == __string__) // but once again slightly askewed + + tasty_fruits[0] = "lemon" + + assert(fruits[0] == __string__) // but once again + assert(fruits[1] == __string__) // some things have changed + assert(fruits[2] == __string__) // since our data is not our own +}