...

Source file src/runtime/trace/example_test.go

Documentation: runtime/trace

		 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  }
		40  

View as plain text