To remove a specific element from an array in Go, you can create a new slice excluding the element to be removed.
In this example,
package main
import (
"fmt"
)
func main() {
numbers := []int{1, 2, 3, 4, 5}
elementToRemove := 3
var newArray []int
for _, num := range numbers {
if num != elementToRemove {
newArray = append(newArray, num)
}
}
fmt.Println("Slice after removing", elementToRemove, ":", newArray)
}
Slice after removing 3 : [1 2 4 5]
In this tutorial, we learned How to Remove Specific Element from an Array in Go language with well detailed examples.