Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Iterate over slice in Go

for range method can be used to iterate over the elements of a slice. Range returns both index and element from the slice.

Example : Range usage with for loop
package main

import "fmt"

func main() {

    carNames := make([]string, 0)

    carNames = append(carNames, "Audi A8", "Mini Cooper")

    for index, carName := range carNames {
        fmt.Printf("Index of car %d, name of car %v \n", index, carName)
    }

}

func removeIndex(slice []string, index int) []string {
    return append(slice[:index], slice[index+1:]...)
}



Output
Index of car 0, name of car Audi A8
Index of car 1, name of car Mini Cooper