Golang runtime.Stack() function example
package runtime
Golang runtime.Stack() function usage example.
package main
import (
"fmt"
"runtime"
)
func echo(s string) {
for i := 0; i < 5; i++ {
runtime.Gosched() // see http://golang.org/pkg/runtime/#Gosched
fmt.Println(s)
}
}
func main() {
// maximize CPU usage for multi threading
runtime.GOMAXPROCS(runtime.NumCPU())
buffer := make([]byte, 4096)
buffer = buffer[:runtime.Stack(buffer, true)]
go echo("bye") // schedule this execution on a new thread [second]
go echo("world") // [third]
echo("good") // current [first thread]
fmt.Println("# go routines : ", runtime.NumGoroutine())
fmt.Println("Stack trace bytes : ")
fmt.Println(buffer)
}
Reference :
Advertisement
Something interesting
Tutorials
+21.8k SSL : How to check if current certificate is sha1 or sha2
+20.9k PHP : Convert(cast) int to double/float
+10k Golang : Read file and convert content to string
+10.9k Golang : Get UDP client IP address and differentiate clients by port number
+7.7k Golang : Command line ticker to show work in progress
+41.4k Golang : Convert string to array/slice
+8.2k Golang : Get final or effective URL with Request.URL example
+15.4k Golang : invalid character ',' looking for beginning of value
+20.2k Golang : Reset or rewind io.Reader or io.Writer
+31.6k Golang : Get local IP and MAC address
+15.9k Golang : Get file permission
+11.9k Golang : Convert decimal number(integer) to IPv4 address