How to delete an element from a Slice in Golang?


RemoveIndex function created to remove specific item from String slice.

Example

package main

import "fmt"

func main() {
	var strSlice = []string{"India", "Canada", "Japan", "Germany", "Italy"}
	fmt.Println(strSlice)

	strSlice = RemoveIndex(strSlice, 3)
	fmt.Println(strSlice)
}

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

Output

[India Canada Japan Germany Italy]
[India Canada Japan Italy]
Most Helpful This Week