Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.