Example: Stack and Caller from RUNTIME package
Stack formats a stack trace of the calling goroutine into Slice and returns the number of bytes written to Slice. Caller reports file and line number information about function invocations on the calling goroutine's stack.
Example
package main
import (
"fmt"
"runtime"
)
func stackExample() {
stackSlice := make([]byte, 512)
s := runtime.Stack(stackSlice, false)
fmt.Printf("\n%s", stackSlice[0:s])
}
func First() {
Second()
}
func Second() {
Third()
}
func Third() {
for c := 0; c < 5; c++ {
fmt.Println(runtime.Caller(c))
}
}
func main() {
fmt.Println("######### STACK ################")
stackExample()
fmt.Println("\n\n######### CALLER ################")
First()
}
Output
######### STACK ################
goroutine 1 [running]:
main.stackExample()
C:/golang/codes/example.go:10 +0x60
main.main()
C:/golang/codes/example.go:30 +0x82
######### CALLER ################
4676922 C:/golang/codes/example.go 24 true
4676855 C:/golang/codes/example.go 19 true
4676823 C:/golang/codes/example.go 15 true
4677435 C:/golang/codes/example.go 32 true
4343718 C:/Go/src/runtime/proc.go 185 true
Most Helpful This Week
How to check UPPERCASE characters in a string in Golang?
Add N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-time
Convert Int data type to Int16 Int32 Int64
How to fetch an Integer variable as String in Go?
How to print struct variables data in Golang?
How to iterate over an Array using for loop?