Skip to content

Instantly share code, notes, and snippets.

@cjgiridhar
Created January 26, 2019 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjgiridhar/25b605075c52c1648e4e21b7cfb9514d to your computer and use it in GitHub Desktop.
Save cjgiridhar/25b605075c52c1648e4e21b7cfb9514d to your computer and use it in GitHub Desktop.
Declaring slices in Golang
package main
import "fmt"
func main() {
slices := []string{"AB", "CD", "EF"}
fmt.Println(slices)
array := [5]int{76, 77, 78, 79, 80}
var slice []int = array[1:4] /*creates a slice from a[1] to a[3] */
/* Returns [77, 78, 79] */
fmt.Println(slice)
vegies := [...]string{"Potato", "Tomato", "Eggplant", "Onion", "Capsicum"}
vegslice := vegies[1:3]
fmt.Printf("Length of slice %d capacity %d", len(vegslice), cap(vegslice))
/* Returns Length of slice 2 capacity 4 */
myslice := make([]int, 3, 3)
fmt.Println(myslice) /* Returns [0 0 0] */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment