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

tour: [SLICE INDEXING CODE NOT VERY EXPLAINATORY OF DESCRIPTION] #1650

Open
fundekaustubh opened this issue Sep 16, 2024 · 0 comments
Open

Comments

@fundekaustubh
Copy link

Context: https://go.dev/tour/moretypes/10

The code for default indexing on slices actually does not illustrate the default beginning and end indices well. As per the code,

s := []int{2, 3, 5, 7, 11, 13}
s = s[1:4]
s = s[:2]
s = s[1:]

which prints

[3 5 7]
[3 5]
[5]

Here, the slicing for the second and third time applies on the updated slice and not the original one. While the output is correct, a better approach, as far as explanation is concerned, might be to create a new slice, store the original slice's in it and display it. For example,

s := []int{2, 3, 5, 7, 11, 13}
t := s[1:4]
t = s[:2]
t = s[1:]

which prints

[3 5 7]
[2 3]
[3 5 7 11 13]

might be the code that illustrates the concepts better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant