Created
July 17, 2021 17:40
-
-
Save alsotang/7708228202b3a99b531359458e89baf2 to your computer and use it in GitHub Desktop.
strange_go_slice.go
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
package main | |
import "fmt" | |
func main() { | |
a := []int{1, 2, 3} | |
b := a[0:2] | |
fmt.Println(cap(a), cap(b)) | |
b[0] = 0 | |
fmt.Println(a, b) | |
b = append(b, 4) | |
fmt.Println(a, b) | |
fmt.Println(cap(a), cap(b)) | |
b = append(b, 4) | |
a[1] = 0 | |
fmt.Println(a, b) | |
fmt.Println(cap(a), cap(b), len(a), len(b)) | |
} | |
// 3 3 | |
// [0 2 3] [0 2] | |
// [0 2 4] [0 2 4] | |
// 3 3 | |
// [0 0 4] [0 2 4 4] | |
// 3 6 3 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment