From 7958694c0ea91d3bef545cc4857a53e8c5eab48d Mon Sep 17 00:00:00 2001
From: adriancuadrado
Date: Mon, 15 Apr 2024 16:34:57 +0200
Subject: [PATCH] Add an example with automatic array size (#522)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* Add am example with automatic array size
You can learn more about this here: https://go.dev/blog/slices-intro#arrays
* Update arrays.sh
Forgot about this file 😅
* Added example with indices in the initialization
* Added multi-dimensional array initialization example
* Removed clarification about commas
* Run tools/build
* Fixed output mismatch
---
examples/arrays/arrays.go | 18 +++++++++++++
examples/arrays/arrays.hash | 4 +--
examples/arrays/arrays.sh | 3 +++
public/arrays | 53 ++++++++++++++++++++++++++++++++++---
4 files changed, 72 insertions(+), 6 deletions(-)
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
|
@@ -123,6 +123,32 @@ in one line.
+
+
+ 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)
}
|
@@ -161,7 +203,10 @@ when printed with 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]]
@@ -180,7 +225,7 @@ when printed with fmt.Println
.