在Go如何从Struct的切片中删除项目

时间:2015-02-07 17:42:28

标签: go slice

我的struct包含另一个struct的切片。我添加了从切片添加和删除项目的方法,但添加工作删除不是。我是Go的新手所以我无法理解为什么切片重新分配不会反映在struct

我的代码的简短版本如下。链接到PlayGoLang:http://play.golang.org/p/4NnGh3Dtzw

 type BatteryTest struct {
    Name, LocalConf, JdkPath, SysProps, LocalconfPath string
    NumberOfNodes                                     int
    }

    type Server struct {
    Port                                    int
    TestQueue, CompletedTests, RunningTests []BatteryTest
}

    func (this *Server) AddBatteryTest(test BatteryTest) error {    
        this.TestQueue = append(this.TestQueue, test)
        return nil
    }

func (this *Server) TakeBatteryTest() error {
    length := len(this.TestQueue)
    if length == 0 {
        fmt.Println("Len==", 0)
        return errors.New("Queue is empty")
    }

    slice := this.TestQueue
    i := len(this.TestQueue) - 1
    slice = append(slice[:i], slice[i+1:]...)
    return nil
}

1 个答案:

答案 0 :(得分:1)

您尚未将slice分配回this.TestQueue