diff --git a/examples/arrays/arrays.go b/examples/arrays/arrays.go index 010a7e564..06b4c17b2 100644 --- a/examples/arrays/arrays.go +++ b/examples/arrays/arrays.go @@ -31,6 +31,16 @@ func main() { b := [5]int{1, 2, 3, 4, 5} fmt.Println("dcl:", b) + // You can also have the compiler count the number of + // elements for you with `...` + b = [...]int{1, 2, 3, 4, 5} + fmt.Println("dcl:", b) + + // If you specify the index with `:`, the elements in + // between will be zeroed. + b = [...]int{100, 3: 400, 500} + fmt.Println("idx:", b) + // Array types are one-dimensional, but you can // compose types to build multi-dimensional data // structures. @@ -41,4 +51,12 @@ func main() { } } fmt.Println("2d: ", twoD) + + // You can create and initialize multi-dimensional + // arrays at once too. + twoD = [2][3]int{ + {1, 2, 3}, + {1, 2, 3}, + } + fmt.Println("2d: ", twoD) } diff --git a/examples/arrays/arrays.hash b/examples/arrays/arrays.hash index 8e1c671d9..12253ebfc 100644 --- a/examples/arrays/arrays.hash +++ b/examples/arrays/arrays.hash @@ -1,2 +1,2 @@ -e2bdc11af83f9c6964cfa0e06e4642943b3055ae -bBVikSoZ1Z7 +789f9faa91c359e5337ace4f80b38428f39d4e7b +zVIFeNnUdwv diff --git a/examples/arrays/arrays.sh b/examples/arrays/arrays.sh index 76c43f6c3..bce55ab8b 100644 --- a/examples/arrays/arrays.sh +++ b/examples/arrays/arrays.sh @@ -6,4 +6,7 @@ set: [0 0 0 0 100] get: 100 len: 5 dcl: [1 2 3 4 5] +dcl: [1 2 3 4 5] +idx: [100 0 0 400 500] 2d: [[0 1 2] [1 2 3]] +2d: [[1 2 3] [1 2 3]] diff --git a/public/arrays b/public/arrays index 675a3f3b9..028fc060f 100644 --- a/public/arrays +++ b/public/arrays @@ -44,7 +44,7 @@ scenarios.
package main
You can also have the compiler count the number of
+elements for you with ...
b = [...]int{1, 2, 3, 4, 5}
+ fmt.Println("dcl:", b)
+ If you specify the index with :
, the elements in
+between will be zeroed.
b = [...]int{100, 3: 400, 500}
+ fmt.Println("idx:", b)
+ Array types are one-dimensional, but you can @@ -130,7 +156,7 @@ compose types to build multi-dimensional data structures.
var twoD [2][3]int
for i := 0; i < 2; i++ {
@@ -138,6 +164,22 @@ structures.
twoD[i][j] = i + j
}
}
+ fmt.Println("2d: ", twoD)
+ You can create and initialize multi-dimensional +arrays at once too.
+ + twoD = [2][3]int{
+ {1, 2, 3},
+ {1, 2, 3},
+ }
fmt.Println("2d: ", twoD)
}
fmt.Println
.
get: 100
len: 5
dcl: [1 2 3 4 5]
-2d: [[0 1 2] [1 2 3]]
+dcl: [1 2 3 4 5]
+idx: [100 0 0 400 500]
+2d: [[0 1 2] [1 2 3]]
+2d: [[1 2 3] [1 2 3]]
fmt.Println
.