Skip to content

Commit

Permalink
Add an example with automatic array size (mmcgrana#522)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
adriancuadrado authored Apr 15, 2024
1 parent f8d6019 commit 7958694
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
18 changes: 18 additions & 0 deletions examples/arrays/arrays.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions examples/arrays/arrays.hash
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
e2bdc11af83f9c6964cfa0e06e4642943b3055ae
bBVikSoZ1Z7
789f9faa91c359e5337ace4f80b38428f39d4e7b
zVIFeNnUdwv
3 changes: 3 additions & 0 deletions examples/arrays/arrays.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
53 changes: 49 additions & 4 deletions public/arrays

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7958694

Please sign in to comment.