slices and arrays

This commit is contained in:
Steven Degutis
2012-03-11 20:56:02 -05:00
parent d8bdbef975
commit 49cd5e3247
3 changed files with 26 additions and 1 deletions

View File

@@ -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
}

View File

@@ -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)
}

18
slices.go Normal file
View File

@@ -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
}