Skip to content

Commit f2c0531

Browse files
andrestchyangah
authored andcommitted
runtime/trace: add example about trace.Start/Stop
This commit adds an example to the runtime/trace package on how to use the trace.Start and trace.Stop functions to trace the execution of a Go program and write its trace output to a file. Change-Id: Idf920398f1c3b9d185af9df5ce9293f2361db022 Reviewed-on: https://go-review.googlesource.com/51170 Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
1 parent 91b6425 commit f2c0531

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/runtime/trace/example_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 trace_test
6+
7+
import (
8+
"fmt"
9+
"log"
10+
"os"
11+
"runtime/trace"
12+
)
13+
14+
// Example demonstrates the use of the trace package to trace
15+
// the execution of a Go program. The trace output will be
16+
// written to the file trace.out
17+
func Example() {
18+
f, err := os.Create("trace.out")
19+
if err != nil {
20+
log.Fatalf("failed to create trace output file: %v", err)
21+
}
22+
defer func() {
23+
if err := f.Close(); err != nil {
24+
log.Fatalf("failed to close trace file: %v", err)
25+
}
26+
}()
27+
28+
if err := trace.Start(f); err != nil {
29+
log.Fatalf("failed to start trace: %v", err)
30+
}
31+
defer trace.Stop()
32+
33+
// your program here
34+
RunMyProgram()
35+
}
36+
37+
func RunMyProgram() {
38+
fmt.Printf("this function will be traced")
39+
}

0 commit comments

Comments
 (0)