Standard library / log

log.Panicf()

Write a message then panic. This example recovers to demonstrate the panic; do not use panic for routine errors. Expected stdout: 'true\ncount=2\n'.

Formal Syntax & Structure

go syntaxgo
func Panicf(format string, v ...any)

Parameters & Attributes

NameTypeDescription
formatstringPrintf-style format string.
v...anyValues to log or convert.

Working Example

Example Snippetgo
package main
import (  "bytes"  "fmt"  "log")
func main() {  var b bytes.Buffer  log.SetOutput(&b)  log.SetFlags(0)  log.SetPrefix("")  defer func() { fmt.Println(recover() != nil); fmt.Print(b.String()) }()  log.Panicf("count=%d", 2)}
Go Reference