Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example with automatic array size #522

Merged
merged 7 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is a good place to add that the array length is the maximum index plus one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that information would add much value since gobyexample is meant for people who already have experience in other programming languages and the length of a list is always the maximum index plus one everywhere.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I mean it specifically in the context of the [...] syntax with explicitly specifying indeces. For example in your second b with the 3:, the array length is 5 because of that.

I don't feel strongly about this, though.

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},
adriancuadrado marked this conversation as resolved.
Show resolved Hide resolved
}
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.

Loading