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
Pass different types of arguments in variadic function
Encoding and Decoding using json.Marshal and json.Unmarshal
Get Hours, Days, Minutes and Seconds difference between two dates [Future and Past]
Constructors in Golang
How to check pointer or interface is nil?
How to Decode or Unmarshal bi-dimensional array of integers?