Created
April 26, 2024 10:21
-
-
Save slash3b/dcdb535cfba61edfa8b6afea87295544 to your computer and use it in GitHub Desktop.
Go slices mind-twister
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| firstSlice := []string{"a", "b", "c", "d", "e", "f"} | |
| firstSlice = firstSlice[:5] | |
| secondSlice := firstSlice | |
| firstSlice = append(firstSlice, "g") | |
| // This will print: [a b c d e g] | |
| fmt.Println(firstSlice) | |
| secondSlice = append(secondSlice, "h") | |
| // This will print: [a b c d e h] | |
| fmt.Println(firstSlice) | |
| // This will print: [a b c d e h] | |
| fmt.Println(secondSlice) | |
| // Bonus: | |
| firstSlice = append(firstSlice, "i") | |
| secondSlice = append(secondSlice, "j") | |
| // This will print: [a b c d e h i] | |
| fmt.Println(firstSlice) | |
| // This will print: [a b c d e h j] | |
| fmt.Println(secondSlice) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment