Golang log.Panic, Panicf and Panicln functions example

package log

Golang log.Panic, Panicf and Panicln functions usage example

 package main

 import (
 "fmt"
 "log"
 )

 func main() {

 defer func() {
 if err := recover(); err != nil {

 fmt.Println(err) // nooo! and stop
 handleException()
 }
 }()

 log.Panic("nooo! and stop")

 // log.Panicf is similar to fmt.Printf ( can replace %s,%v,%d with values)

 // log.Panicln is similar to fmt.Println (end of with \n)

 log.Println("this will not be called.")
 }

 func handleException() {
 log.Println("recovering...")
 }

References :

How to use panic and recover

http://golang.org/pkg/log/#Panic

http://golang.org/pkg/log/#Panicf

http://golang.org/pkg/log/#Panicln

Advertisement