1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 func ExampleFrames() {
15 // Ask runtime.Callers for up to 10 PCs, including runtime.Callers itself.
16 pc := make([]uintptr, 10)
17 n := runtime.Callers(0, pc)
19 // No PCs available. This can happen if the first argument to
20 // runtime.Callers is large.
22 // Return now to avoid processing the zero Frame that would
23 // otherwise be returned by frames.Next below.
27 pc = pc[:n] // pass only valid pcs to runtime.CallersFrames
28 frames := runtime.CallersFrames(pc)
30 // Loop to get frames.
31 // A fixed number of PCs can expand to an indefinite number of Frames.
33 frame, more := frames.Next()
35 // Process this frame.
37 // To keep this example's output stable
38 // even if there are changes in the testing package,
39 // stop unwinding when we leave package runtime.
40 if !strings.Contains(frame.File, "runtime/") {
43 fmt.Printf("- more:%v | %s\n", more, frame.Function)
45 // Check whether there are more frames to process after this one.
57 // - more:true | runtime.Callers
58 // - more:true | runtime_test.ExampleFrames.func1
59 // - more:true | runtime_test.ExampleFrames.func2
60 // - more:true | runtime_test.ExampleFrames.func3
61 // - more:true | runtime_test.ExampleFrames