DEV Community

vinay
vinay

Posted on

How to insert element at any position in Slice Golang

#go
func Insert(array []int, element int, i int) []int {
    return append(array[:i], append([]int{element}, array[i:]...)...)
}
func main() {
    a := []int{10, 20, 30, 40}
    a = Insert(a, 30, len(a))
    fmt.Println(a)
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)