Golang - Delete the element from the given slice Code Example

The code for Delete the element from the given slice

package main
import "fmt"
 
func main() {
    // Slice
    numbers := []int{10, 15, 30, 40, 20,10, 80}
    fmt.Println("Original Slice:", numbers)
     
    var index int = 3
    elem := numbers[index]
     
    // Using append function to combine two slices
    numbers = append(numbers[:index], numbers[index+1:]...)
 
    fmt.Printf("The element %d was deleted.\n", elem)
    fmt.Println("Slice after deleting elements:", numbers)
}

/*
Output:

Original Slice: [10 15 30 40 20 10 80]
The element 40 was deleted.
Slice after deleting elements: [10 15 30 20 10 80]

*/
Code by IncludeHelp, on February 28, 2023 15:36

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.