我想实现基于时间的插槽,以便使用golang切片保存数据。我设法提出了一个go程序,它也可以工作。但是我对垃圾回收和该程序的一般性能没有疑问。切片等于nil时,此程序是否保证对项目进行垃圾回收?而且,在重新整理切片时,希望该程序不要进行任何深层复制。
type DataSlots struct {
slotDuration int //in milliseconds
slots [][]interface{}
totalDuration int //in milliseconds
}
func New(slotDur int, totalDur int) *DataSlots {
dat := &DataSlots{slotDuration: slotDur,
totalDuration: totalDur}
n := totalDur / slotDur
dat.slots = make([][]interface{}, n)
for i := 0; i < n; i++ {
dat.slots[i] = make([]interface{}, 0)
}
go dat.manageSlots()
return dat
}
func (self *DataSlots) addData(data interface{}) {
self.slots[0] = append(self.slots[0], data)
}
// This should be a go routine
func (self *DataSlots) manageSlots() { …Run Code Online (Sandbox Code Playgroud)