How to remove element of struct array in loop in golang

前端 未结 4 1710
谎友^
谎友^ 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:51

    I think the simple way is

    var (
      slice = []int{1,2,3,4,5}
      pos int
    )
        for _, i := range slice {
            if i == 3 {
                slice = append(slice[:pos], slice[pos+1:]...)
                if pos > 0 {
                    pos = pos - 1
                }
                continue
            }
            pos++
        }
    

    here is... https://play.golang.org/p/pK3B5Mii9k

提交回复
热议问题