Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a safe way to empty a list in Golang?

Tags:

list

go

An traditional way:

// Clear all elements by iterating
var next *Element
for e := l.Front(); e != nil; e = next {
    next = e.Next()
    l.Remove(e)
}

How about using:

l.Init()

Is it a safe way that will not causing any memory leak?

like image 710
viechang Avatar asked Mar 20 '23 05:03

viechang


1 Answers

From http://golang.org/pkg/container/list/#List.Init

Init initializes or clears list l.

A side note, a slice is probably better for most usage scenarios, check Slice Tricks.

like image 154
OneOfOne Avatar answered Mar 21 '23 17:03

OneOfOne