MCPcopy Create free account
hub / github.com/ardanlabs/gotraining / Pop

Method Pop

topics/go/algorithms/data/stack/stack.go:39–54  ·  view source on GitHub ↗

Pop removes data from the top of the stack.

()

Source from the content-addressed store, hash-verified

37
38// Pop removes data from the top of the stack.
39func (s *Stack) Pop() (*Data, error) {
40 if len(s.data) == 0 {
41 return nil, errors.New("stack empty")
42 }
43
44 // Calculate the top level index.
45 idx := len(s.data) - 1
46
47 // Copy the data from that index position.
48 data := s.data[idx]
49
50 // Remove the top level index from the slice.
51 s.data = s.data[:idx]
52
53 return data, nil
54}
55
56// Peek provides the data stored on the stack based
57// on the level from the bottom. A value of 0 would

Callers 1

TestPopFunction · 0.95

Calls

no outgoing calls

Tested by 1

TestPopFunction · 0.76