DEV Community

vinay
vinay

Posted on

3

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)

AI Agent image

How to Build an AI Agent with Semantic Kernel (and More!)

Join Developer Advocate Luce Carter for a hands-on tutorial on building an AI-powered dinner recommendation agent. Discover how to integrate Microsoft Semantic Kernel, MongoDB Atlas, C#, and OpenAI for ingredient checks and smart restaurant suggestions.

Watch the video 📺