相关疑难解决方法(0)

如何在golang中删除循环中struct数组的元素

问题

我有一些结构:

type Config struct {
  Applications []Application
}
Run Code Online (Sandbox Code Playgroud)

注意:Config - 是json.Decode的结构.

config = new(Config)
_ = decoder.Decode(&config)
Run Code Online (Sandbox Code Playgroud)

在循环中,我有一些条件和按键删除元素.

for i, application := range config.Applications {
  if i == 1 {
    config.Applications = _removeApplication(i, config.Applications)
  }
}

func _removeApplication(i int, list []Application) []Application {
  if i < len(list)-1 {
    list = append(list[:i], list[i+1:]...)
  } else {
    log.Print(list[i].Name)
    list = list[:i]
  }

  return list
}
Run Code Online (Sandbox Code Playgroud)

但总是我有"超出范围"的错误.从结构数组中按键删除元素的最佳方法是什么?

arrays struct go slice

16
推荐指数
2
解决办法
2万
查看次数

标签 统计

arrays ×1

go ×1

slice ×1

struct ×1