aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/example_test.go
diff options
context:
space:
mode:
authorJosh Bleecher Snyder <josharian@gmail.com>2017-02-11 14:56:16 -0800
committerJosh Bleecher Snyder <josharian@gmail.com>2017-02-13 06:10:35 +0000
commit8da91a6297d5960b69ca22d764ef73906f6d61e9 (patch)
treea8955873b81333e24ddcfb9e16ebfa1ab53911e7 /src/runtime/example_test.go
parent39fcf8bf0e3a3b447780983c1f88df5bb9f5bc98 (diff)
downloadgo-8da91a6297d5960b69ca22d764ef73906f6d61e9.tar.gz
go-8da91a6297d5960b69ca22d764ef73906f6d61e9.zip
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>
Diffstat (limited to 'src/runtime/example_test.go')
-rw-r--r--src/runtime/example_test.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/runtime/example_test.go b/src/runtime/example_test.go
new file mode 100644
index 0000000000..f817b595e6
--- /dev/null
+++ b/src/runtime/example_test.go
@@ -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
+}