mirror of
https://github.com/golang/go
synced 2024-11-02 11:50:30 +00:00
runtime: add Frames example
Based on sample code from iant. Fixes #18788. Change-Id: I6bb33ed05af2538fbde42ddcac629280ef7c00a6 Reviewed-on: https://go-review.googlesource.com/36892 Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
39fcf8bf0e
commit
8da91a6297
1 changed files with 39 additions and 0 deletions
39
src/runtime/example_test.go
Normal file
39
src/runtime/example_test.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
// Copyright 2017 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package runtime_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func ExampleFrames() {
|
||||
c := func() {
|
||||
pc := make([]uintptr, 5)
|
||||
n := runtime.Callers(0, pc)
|
||||
if n == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
frames := runtime.CallersFrames(pc[:n])
|
||||
var frame runtime.Frame
|
||||
more := true
|
||||
for more {
|
||||
frame, more = frames.Next()
|
||||
fmt.Printf("- more:%v | %s\n", more, frame.Function)
|
||||
}
|
||||
}
|
||||
|
||||
b := func() { c() }
|
||||
a := func() { b() }
|
||||
|
||||
a()
|
||||
// Output:
|
||||
// - more:true | runtime.Callers
|
||||
// - more:true | runtime_test.ExampleFrames.func1
|
||||
// - more:true | runtime_test.ExampleFrames.func2
|
||||
// - more:true | runtime_test.ExampleFrames.func3
|
||||
// - more:false | runtime_test.ExampleFrames
|
||||
}
|
Loading…
Reference in a new issue