1
0
mirror of https://github.com/golang/go synced 2024-11-25 09:07:58 -07:00

container/list: make Remove return Value of removed element.

When it is known that there is already at least one element in the
list, it is awkwardly verbose to use three lines and an extra
variable declaration to remove the first or last item (a common
case), rather than use a simple expression.

a stack:
	stk.PushFront(x)
	x = stk.Front().Remove().(T)

vs.
	stk.PushFront(x)
	e := stk.Front()
	e.Remove()
	x = e.Value.(T)
[An alternative CL might be to add PopFront and PopBack methods].

R=gri
CC=golang-dev
https://golang.org/cl/3000041
This commit is contained in:
Roger Peppe 2010-11-09 08:58:23 -08:00 committed by Robert Griesemer
parent 9626180918
commit e9afb9d399

View File

@ -54,10 +54,12 @@ func (l *List) Front() *Element { return l.front }
// Back returns the last element in the list. // Back returns the last element in the list.
func (l *List) Back() *Element { return l.back } func (l *List) Back() *Element { return l.back }
// Remove removes the element from the list. // Remove removes the element from the list
func (l *List) Remove(e *Element) { // and returns its Value.
func (l *List) Remove(e *Element) interface{} {
l.remove(e) l.remove(e)
e.list = nil // do what remove does not e.list = nil // do what remove does not
return e.Value
} }
// remove the element from the list, but do not clear the Element's list field. // remove the element from the list, but do not clear the Element's list field.