Created
January 10, 2023 17:22
-
-
Save pkbhowmick/fc4166b267fa75945b38e65638f45231 to your computer and use it in GitHub Desktop.
Slice in Golang
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() { | |
nums := []int{1, 2, 3, 4, 5, 6, 7, 8} | |
printSlice(nums) | |
nums = append(nums, 9) | |
printSlice(nums) | |
nums = nums[:0] | |
printSlice(nums) | |
nums = nums[:6] | |
printSlice(nums) | |
nums = nums[5:] | |
printSlice(nums) | |
} | |
func printSlice(s []int) { | |
fmt.Printf("len=%v, cap=%v, elements=%v\n", len(s), cap(s), s) | |
fmt.Print("value addresses:") | |
for i := range s { | |
fmt.Print(" ", &s[i]) | |
} | |
fmt.Println() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment