| 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. |
| 4 |
|
| 5 |
package runtime_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"os" |
| 10 |
"runtime" |
| 11 |
"strings" |
| 12 |
) |
| 13 |
|
| 14 |
func ExampleFrames() { |
| 15 |
c := func() { |
| 16 |
// Ask runtime.Callers for up to 10 PCs, including runtime.Callers itself. |
| 17 |
pc := make([]uintptr, 10) |
| 18 |
n := runtime.Callers(0, pc) |
| 19 |
if n == 0 { |
| 20 |
// No PCs available. This can happen if the first argument to |
| 21 |
// runtime.Callers is large. |
| 22 |
// |
| 23 |
// Return now to avoid processing the zero Frame that would |
| 24 |
// otherwise be returned by frames.Next below. |
| 25 |
return |
| 26 |
} |
| 27 |
|
| 28 |
pc = pc[:n] // pass only valid pcs to runtime.CallersFrames |
| 29 |
frames := runtime.CallersFrames(pc) |
| 30 |
|
| 31 |
// Loop to get frames. |
| 32 |
// A fixed number of PCs can expand to an indefinite number of Frames. |
| 33 |
for { |
| 34 |
frame, more := frames.Next() |
| 35 |
|
| 36 |
// Canonicalize function name and skip callers of this function |
| 37 |
// for predictable example output. |
| 38 |
// You probably don't need this in your own code. |
| 39 |
function := strings.ReplaceAll(frame.Function, "main.main", "runtime_test.ExampleFrames") |
| 40 |
fmt.Printf("- more:%v | %s\n", more, function) |
| 41 |
if function == "runtime_test.ExampleFrames" { |
| 42 |
break |
| 43 |
} |
| 44 |
|
| 45 |
// Check whether there are more frames to process after this one. |
| 46 |
if !more { |
| 47 |
break |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
b := func() { c() } |
| 53 |
a := func() { b() } |
| 54 |
|
| 55 |
a() |
| 56 |
// Output: |
| 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 |
| 62 |
} |
| 63 |
|
| 64 |
func ExampleAddCleanup() { |
| 65 |
tempFile, err := os.CreateTemp(os.TempDir(), "file.*") |
| 66 |
if err != nil { |
| 67 |
fmt.Println("failed to create temp file:", err) |
| 68 |
return |
| 69 |
} |
| 70 |
|
| 71 |
ch := make(chan struct{}) |
| 72 |
|
| 73 |
// Attach a cleanup function to the file object. |
| 74 |
runtime.AddCleanup(&tempFile, func(fileName string) { |
| 75 |
if err := os.Remove(fileName); err == nil { |
| 76 |
fmt.Println("temp file has been removed") |
| 77 |
} |
| 78 |
ch <- struct{}{} |
| 79 |
}, tempFile.Name()) |
| 80 |
|
| 81 |
if err := tempFile.Close(); err != nil { |
| 82 |
fmt.Println("failed to close temp file:", err) |
| 83 |
return |
| 84 |
} |
| 85 |
|
| 86 |
// Run the garbage collector to reclaim unreachable objects |
| 87 |
// and enqueue their cleanup functions. |
| 88 |
runtime.GC() |
| 89 |
|
| 90 |
// Wait until cleanup function is done. |
| 91 |
<-ch |
| 92 |
|
| 93 |
// Output: |
| 94 |
// temp file has been removed |
| 95 |
} |