Created
March 22, 2018 16:42
-
-
Save c-yan/c20145ba9799f0e37cd0b0c683245508 to your computer and use it in GitHub Desktop.
slice shrink
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() { | |
array := make([]int, 0, 0) | |
for i := 0; i < 1000; i++ { | |
array = append(array, i) | |
if i%100 == 0 { | |
fmt.Printf("ptr=%p len=%-3d cap=%d\n", &array[0], len(array), cap(array)) | |
} | |
} | |
array = array[:0] | |
fmt.Println("-------") | |
for i := 0; i < 1000; i++ { | |
array = append(array, i) | |
if i%100 == 0 { | |
fmt.Printf("ptr=%p len=%-3d cap=%d\n", &array[0], len(array), cap(array)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Execution Result: