Avatar ·

How to delete an element from a slice in Golang? Correct ways and pitfalls

Hello everyone! I just started learning Go and encountered a problem when working with slices. I need to delete an element by index from a slice, but I can't find a built-in function for this. Here's an example of my code: ```go package main import "fmt" func main() { fruits := []string{"apple", "banana", "cherry", "date"} // I want to delete "banana" (index 1) // How to do this correctly? fmt.Println(fruits) } ``` I tried simply assigning nil to the element, but that doesn't delete it from the slice, it only zeroes the value: ```go fruits[1] = "" // Doesn't work as deletion ``` Then I tried making a slice without the needed element, but this creates a new slice while the original remains: ```go newFruits := append(fruits[:1], fruits[2:]...) // Now fruits = ["apple"