Created
February 14, 2024 15:13
-
-
Save yakuter/d1893a0311f1c14b1390def3332d743f to your computer and use it in GitHub Desktop.
Copy and Movement 2
This file contains 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
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