Standard library / runtime

runtime.Stack()

Writes the current goroutine stack, or all goroutine stacks when all is true, into a caller-supplied buffer. Output may truncate and contain sensitive details. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func Stack(buf []byte, all bool) int

Parameters & Attributes

NameTypeDescription
buf[]byteDestination byte buffer; choose sufficient capacity for the desired trace.
allboolTrue includes other goroutines; false requests only the calling goroutine.

Return Value

int

Number of bytes written into buf.

Working Example

Example Snippetgo
package main
import (  "fmt"  "runtime"  "strings")
func main() {  buf := make([]byte, 8192)  n := runtime.Stack(buf, false)  fmt.Println(strings.Contains(string(buf[:n]), "main.main"))}
Go Reference