From 62333fb9cd1460fc19567d808aa13082b101cc16 Mon Sep 17 00:00:00 2001 From: Steven Degutis Date: Sun, 11 Mar 2012 18:24:25 -0500 Subject: [PATCH] strings rock --- arrays.go | 3 --- basics.go | 5 ++--- setup_koans_test.go | 8 ++++++-- strings.go | 30 +++++++++++++++++++++++++++--- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/arrays.go b/arrays.go index 5f8c442..132c137 100644 --- a/arrays.go +++ b/arrays.go @@ -1,8 +1,5 @@ package go_koans -var __string__ string = "impossibly lame value" -var __int__ int = -1 - func testArrays() { fruits := [4]string{"apple", "orange", "mango"} diff --git a/basics.go b/basics.go index 09b06da..01ab1a3 100644 --- a/basics.go +++ b/basics.go @@ -1,8 +1,7 @@ package go_koans -var __ bool - func testBasics() { - //assert(__ == true) // what is truth? + assert(__bool__ == true) // what is truth? + //assert(!__ != true) // in it there is nothing false } diff --git a/setup_koans_test.go b/setup_koans_test.go index febb2b5..75160d8 100644 --- a/setup_koans_test.go +++ b/setup_koans_test.go @@ -10,10 +10,14 @@ import ( "strings" ) +var __string__ string = "impossibly lame value" +var __int__ int = -1 +var __bool__ bool = false + func TestKoans(t *testing.T) { - testBasics() + //testBasics() testStrings() - testArrays() + //testArrays() fmt.Printf("\n%c[32;1mYou won life. Good job.\n\n", 27) } diff --git a/strings.go b/strings.go index b5d5d92..f746410 100644 --- a/strings.go +++ b/strings.go @@ -1,7 +1,31 @@ package go_koans -func testStrings() { - //var __ string +import "fmt" - //assert("a" + __ == "abc") +func testStrings() { + assert("a" + "bc" == "abc") // string concatenation need not be difficult + assert(len("abc") == 3) // and bounds are thoroughly checked at compile time + + assert("abc"[0] == 'a') // their contents are reminiscent of C + + assert("smith"[2:] == "ith") // slicing may omit the end point + assert("smith"[:4] == "smit") // or the beginning + assert("smith"[2:4] == "it") // or neither + assert("smith"[:] == "smith") // or both + + assert("smith" == "smith") // they can be compared directly + assert("smith" > "foo") // and allow correct but generally useless comparisons + assert("smith" < "zoo") // i suppose maybe this could be useful.. someday + + bytes := []byte{'a', 'b', 'c'} + assert(string(bytes) == "abc") // strings can be created from byte-slices + + bytes[0] = 'z' + assert(string(bytes) == "zbc") // byte-slices can be mutated, although strings cannot + + assert(fmt.Sprintf("hello %s", "world") == "hello world") // our old friend sprintf returns + assert(fmt.Sprintf("hello \"%s\"", "world") == "hello \"world\"") // quoting is familiar + assert(fmt.Sprintf("hello %q", "world") == "hello \"world\"") // although it can be done easilier + + assert(fmt.Sprintf("your balance: %d and %0.2f", 3, 4.5589) == "your balance: 3 and 4.56") // "the root of all evil" is actually a misquotation, by the way }