golang for slice code example
Example 1: golang iterate through slice
var slice = []string{"Apple", "Orange", "Kiwi"}
for index, sliceItem := range slice {
fmt.Println("Index in slice:", index)
fmt.Println("Fruit:", sliceItem)
}
Example 2: golang slice
package main
import (
"fmt"
)
func main() {
var slice = []int{1, 2, 3, 4, 5}
var store = make([]int, 3)
push := append(slice, 6, 7, 8, 9)
copy(store, slice)
fmt.Println("before append value", slice)
fmt.Println("after append value", push)
fmt.Println("render value all value exist", push[:])
fmt.Println("render value after second value", push[2:])
fmt.Println("render value before third index", push[:3])
fmt.Println("render value start from zero and last value before second index", push[0:2])
fmt.Println("merge slice value", store)
fmt.Println("read size slice", len(slice))
fmt.Println("read capacity slice", cap(slice))
}