How to remove element of struct array in loop in golang

前端 未结 4 1724
谎友^
谎友^ 2020-12-29 06:01

Problem

I have array of structs:

type Config struct {
  Applications []Application
}

Note: Config - is a struct fo

4条回答
  •  灰色年华
    2020-12-29 06:46

    You are getting this error because you are doing a loop over a slice with an inital range of X length that became X-n because you remove some elements during loop.

    If you want to delete an item at a specific index from a slice, you can do it this way:

    sliceA = append(sliceA[:indexOfElementToRemove], sliceA[indexOfElementToRemove+1:]...)
    

提交回复
热议问题