Skip to content

Instantly share code, notes, and snippets.

@slash3b
Created April 26, 2024 10:21
Show Gist options
  • Select an option

  • Save slash3b/dcdb535cfba61edfa8b6afea87295544 to your computer and use it in GitHub Desktop.

Select an option

Save slash3b/dcdb535cfba61edfa8b6afea87295544 to your computer and use it in GitHub Desktop.
Go slices mind-twister
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