Created
November 6, 2022 18:37
-
-
Save ekusiadadus/a22c6c5481435c69f0bc6fc8330fef60 to your computer and use it in GitHub Desktop.
slice initialization Golang
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 a []int | |
a = make([]int, 5) | |
for i := 0; i < 5; i++ { | |
a = append(a, i) | |
fmt.Println(a) | |
} | |
fmt.Printf("len=%d cap=%d slice=%v", len(a), cap(a), a) | |
a = make([]int, 0, 5) | |
for i := 0; i < 5; i++ { | |
a = append(a, i) | |
fmt.Println(a) | |
} | |
fmt.Printf("len=%d cap=%d slice=%v", len(a), cap(a), a) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment