Skip to content

Commit 432d0d5

Browse files
committed
Improve documentation and add example.
1 parent 773c2b5 commit 432d0d5

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

format_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package stack_test
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/go-stack/stack"
7+
)
8+
9+
func Example_callFormat() {
10+
log("%+s")
11+
log("%v %[1]n()")
12+
// Output:
13+
// github.com/go-stack/stack/format_test.go
14+
// format_test.go:11 Example_callFormat()
15+
}
16+
17+
func log(format string) {
18+
fmt.Printf(format+"\n", stack.Caller(1))
19+
}

stack.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
// Package stack implements utilities to capture, manipulate, and format call
2-
// stacks.
2+
// stacks. It provides a simpler API than package runtime.
3+
//
4+
// The implementation takes care of the minutia and special cases of
5+
// interpreting the program counter (pc) values returned by runtime.Callers.
6+
//
7+
// Package stack's types implement fmt.Formatter, which provides a simple and
8+
// flexible way to declaratively configure formatting when used with logging
9+
// or error tracking packages.
310
package stack
411

512
import (
@@ -234,7 +241,7 @@ func Trace() CallStack {
234241
return cs
235242
}
236243

237-
// TrimBelow returns a slice of the CallStack with all entries below pc
244+
// TrimBelow returns a slice of the CallStack with all entries below c
238245
// removed.
239246
func (cs CallStack) TrimBelow(c Call) CallStack {
240247
for len(cs) > 0 && cs[0].pc != c.pc {
@@ -243,7 +250,7 @@ func (cs CallStack) TrimBelow(c Call) CallStack {
243250
return cs
244251
}
245252

246-
// TrimAbove returns a slice of the CallStack with all entries above pc
253+
// TrimAbove returns a slice of the CallStack with all entries above c
247254
// removed.
248255
func (cs CallStack) TrimAbove(c Call) CallStack {
249256
for len(cs) > 0 && cs[len(cs)-1].pc != c.pc {

0 commit comments

Comments
 (0)