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
Create and Print Multi Dimensional Slice in Golang
Example to create custom error
How to iterate over a Map using for loop in Go?
How to fetch an Integer variable as String in Go?
Regex to extract image name from HTML in Golang
Get Hours, Days, Minutes and Seconds difference between two dates [Future and Past]