Skip to content

Instantly share code, notes, and snippets.

@pkbhowmick
Created January 10, 2023 17:22
Show Gist options
  • Save pkbhowmick/fc4166b267fa75945b38e65638f45231 to your computer and use it in GitHub Desktop.
Save pkbhowmick/fc4166b267fa75945b38e65638f45231 to your computer and use it in GitHub Desktop.
Slice in Golang
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