strings are done-ish

This commit is contained in:
Steven Degutis
2012-03-11 18:50:08 -05:00
parent 62333fb9cd
commit 5b9963f88d
2 changed files with 16 additions and 16 deletions

View File

@@ -12,6 +12,7 @@ import (
var __string__ string = "impossibly lame value"
var __int__ int = -1
var __byte__ byte = 255
var __bool__ bool = false
func TestKoans(t *testing.T) {

View File

@@ -3,29 +3,28 @@ package go_koans
import "fmt"
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("a" + __string__ == "abc") // string concatenation need not be difficult
assert(len("abc") == __int__) // and bounds are thoroughly checked at compile time
assert("abc"[0] == 'a') // their contents are reminiscent of C
assert("abc"[0] == __byte__) // 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"[2:] == __string__) // slicing may omit the end point
assert("smith"[:4] == __string__) // or the beginning
assert("smith"[2:4] == __string__) // or neither
assert("smith"[:] == __string__) // 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
assert("smith" == __string__) // they can be compared directly
assert("smith" < __string__) // i suppose maybe this could be useful.. someday
bytes := []byte{'a', 'b', 'c'}
assert(string(bytes) == "abc") // strings can be created from byte-slices
assert(string(bytes) == __string__) // strings can be created from byte-slices
bytes[0] = 'z'
assert(string(bytes) == "zbc") // byte-slices can be mutated, although strings cannot
assert(string(bytes) == __string__) // 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("hello %s", __string__) == "hello world") // our old friend sprintf returns
assert(fmt.Sprintf("hello \"%s\"", "world") == __string__) // quoting is familiar
assert(fmt.Sprintf("hello %q", "world") == __string__) // 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
assert(fmt.Sprintf("your balance: %d and %0.2f", 3, 4.5589) == __string__) // "the root of all evil" is actually a misquotation, by the way
}