Skip to content

Instantly share code, notes, and snippets.

@yakuter
Created February 14, 2024 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yakuter/d1893a0311f1c14b1390def3332d743f to your computer and use it in GitHub Desktop.
Save yakuter/d1893a0311f1c14b1390def3332d743f to your computer and use it in GitHub Desktop.
Copy and Movement 2
package main
import "fmt"
func main() {
var s []int
// len=0 cap=0 []
s = append(s, 0) // append works on nil slices.
// len=1 cap=1 [0]
s = append(s, 1) // The slice grows as needed.
// len=2 cap=2 [0 1]
s = append(s, 2, 3, 4) // We can add more than one element at a time.
// len=5 cap=6 [0 1 2 3 4]
s = append(s, 5, 6)
// len=7 cap=12 [0 1 2 3 4 5 6]
anotherSlice := []int{7, 8}
s = append(s, anotherSlice...) // We can combine two slices
// len=9 cap=12 [0 1 2 3 4 5 6 7 8]
}
func printSlice(s []int) {
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment