Created
January 26, 2019 02:38
-
-
Save cjgiridhar/25b605075c52c1648e4e21b7cfb9514d to your computer and use it in GitHub Desktop.
Declaring slices 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() { | |
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