From 010d265aa8e6f497f5f3579180f920ca7b7bd5de Mon Sep 17 00:00:00 2001 From: adriancuadrado Date: Sun, 14 Apr 2024 18:58:19 +0200 Subject: [PATCH 1/7] Add am example with automatic array size You can learn more about this here: https://go.dev/blog/slices-intro#arrays --- examples/arrays/arrays.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/arrays/arrays.go b/examples/arrays/arrays.go index 010a7e564..0a50741a9 100644 --- a/examples/arrays/arrays.go +++ b/examples/arrays/arrays.go @@ -31,6 +31,11 @@ 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) + // Array types are one-dimensional, but you can // compose types to build multi-dimensional data // structures. From bbf6b805778d184b48660b430989b8c027ea13c9 Mon Sep 17 00:00:00 2001 From: adriancuadrado Date: Sun, 14 Apr 2024 19:10:14 +0200 Subject: [PATCH 2/7] Update arrays.sh MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Forgot about this file 😅 --- examples/arrays/arrays.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/arrays/arrays.sh b/examples/arrays/arrays.sh index 76c43f6c3..537a5cca5 100644 --- a/examples/arrays/arrays.sh +++ b/examples/arrays/arrays.sh @@ -6,4 +6,5 @@ set: [0 0 0 0 100] get: 100 len: 5 dcl: [1 2 3 4 5] +dcl: [1 2 3 4 5] 2d: [[0 1 2] [1 2 3]] From 9c8890938ec1372749b96073af0411a910d679f0 Mon Sep 17 00:00:00 2001 From: adriancuadrado Date: Sun, 14 Apr 2024 19:44:27 +0200 Subject: [PATCH 3/7] Added example with indices in the initialization --- examples/arrays/arrays.go | 5 +++++ examples/arrays/arrays.sh | 1 + 2 files changed, 6 insertions(+) diff --git a/examples/arrays/arrays.go b/examples/arrays/arrays.go index 0a50741a9..268b1f98b 100644 --- a/examples/arrays/arrays.go +++ b/examples/arrays/arrays.go @@ -36,6 +36,11 @@ func main() { 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(b) + // Array types are one-dimensional, but you can // compose types to build multi-dimensional data // structures. diff --git a/examples/arrays/arrays.sh b/examples/arrays/arrays.sh index 537a5cca5..2330f1116 100644 --- a/examples/arrays/arrays.sh +++ b/examples/arrays/arrays.sh @@ -7,4 +7,5 @@ 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]] From deeb03a272818eff07b10676b5e56043f6a5eed4 Mon Sep 17 00:00:00 2001 From: adriancuadrado Date: Sun, 14 Apr 2024 20:05:22 +0200 Subject: [PATCH 4/7] Added multi-dimensional array initialization example --- examples/arrays/arrays.go | 15 +++++++++++++-- examples/arrays/arrays.sh | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/examples/arrays/arrays.go b/examples/arrays/arrays.go index 268b1f98b..efde10fe5 100644 --- a/examples/arrays/arrays.go +++ b/examples/arrays/arrays.go @@ -38,8 +38,8 @@ func main() { // If you specify the index with `:`, the elements in // between will be zeroed. - b := [...]int{100, 3: 400, 500} - fmt.Println(b) + 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 @@ -51,4 +51,15 @@ 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}, // Notice this comma here. It's necessary + // because you will get this error otherwise: + // syntax error: unexpected newline in composite + // literal; possibly missing comma or } + } + fmt.Println("2d: ", twoD) } diff --git a/examples/arrays/arrays.sh b/examples/arrays/arrays.sh index 2330f1116..74205cf7d 100644 --- a/examples/arrays/arrays.sh +++ b/examples/arrays/arrays.sh @@ -9,3 +9,4 @@ 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] [4 5 6]] From d4a9e6928344b260fe76a8a04606f58deb94cba3 Mon Sep 17 00:00:00 2001 From: adriancuadrado Date: Mon, 15 Apr 2024 15:07:29 +0200 Subject: [PATCH 5/7] Removed clarification about commas --- examples/arrays/arrays.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/examples/arrays/arrays.go b/examples/arrays/arrays.go index efde10fe5..06b4c17b2 100644 --- a/examples/arrays/arrays.go +++ b/examples/arrays/arrays.go @@ -56,10 +56,7 @@ func main() { // arrays at once too. twoD = [2][3]int{ {1, 2, 3}, - {1, 2, 3}, // Notice this comma here. It's necessary - // because you will get this error otherwise: - // syntax error: unexpected newline in composite - // literal; possibly missing comma or } + {1, 2, 3}, } fmt.Println("2d: ", twoD) } From 0dfcb63ac20c33742c4b1d61df66392703553a94 Mon Sep 17 00:00:00 2001 From: adriancuadrado Date: Mon, 15 Apr 2024 13:29:17 +0000 Subject: [PATCH 6/7] Run tools/build --- examples/arrays/arrays.hash | 4 +-- public/arrays | 53 ++++++++++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 6 deletions(-) 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/public/arrays b/public/arrays index 675a3f3b9..25a5695a5 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] [4 5 6]] @@ -180,7 +225,7 @@ when printed with fmt.Println.

From 68dde2821b7e86ed3269fd4925952c14205b0596 Mon Sep 17 00:00:00 2001 From: adriancuadrado Date: Mon, 15 Apr 2024 14:14:28 +0000 Subject: [PATCH 7/7] Fixed output mismatch --- examples/arrays/arrays.sh | 2 +- public/arrays | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/arrays/arrays.sh b/examples/arrays/arrays.sh index 74205cf7d..bce55ab8b 100644 --- a/examples/arrays/arrays.sh +++ b/examples/arrays/arrays.sh @@ -9,4 +9,4 @@ 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] [4 5 6]] +2d: [[1 2 3] [1 2 3]] diff --git a/public/arrays b/public/arrays index 25a5695a5..028fc060f 100644 --- a/public/arrays +++ b/public/arrays @@ -206,7 +206,7 @@ when printed with fmt.Println.

dcl: [1 2 3 4 5] idx: [100 0 0 400 500] 2d: [[0 1 2] [1 2 3]] -2d: [[1 2 3] [4 5 6]] +2d: [[1 2 3] [1 2 3]]