Standard library / runtime/debug

debug.SetCrashOutput()

Configures an additional file for fatal crash output, duplicating its descriptor. Pass nil with options to disable; this example verifies configuration and cleanup, not a crash. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func SetCrashOutput(f *os.File, opts CrashOptions) error

Parameters & Attributes

NameTypeDescription
f*os.FileAdditional output file, or nil to disable it. The descriptor is duplicated.
optsCrashOptionsCrash output options; the Go 1.23.2 type has no exposed fields.

Return Value

error

Nil on successful configuration, otherwise an error.

Working Example

Example Snippetgo
package main
import (  "fmt"  "os"  "runtime/debug")
func main() {  f, err := os.CreateTemp("", "crash-*")  if err != nil {    panic(err)  }  defer os.Remove(f.Name())  err = debug.SetCrashOutput(f, debug.CrashOptions{})  f.Close()  if err != nil {    panic(err)  }  fmt.Println(debug.SetCrashOutput(nil, debug.CrashOptions{}) == nil)}
Go Reference