Home »
Golang »
Golang Programs
Golang program to demonstrate the log.Panic() function
Here, we are going to demonstrate the log.Panic() function in Golang (Go Language).
Submitted by Nidhi, on April 22, 2021 [Last updated : March 05, 2023]
log.Panic() function in Golang
Problem Solution:
In this program, we will use log.Panic() function to print specified message with timestamp on the console screen. The log.Panic() is similar to the log.Print() function followed by a call to Panic() function.
Program/Source Code:
The source code to demonstrate the log.Panic() function is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.
Golang code to demonstrate the example of log.Panic() function
// Golang program to demonstrate
// the log.Panic() function
package main
// Import log package to use
// Panic() function
import "log"
// Import fmt package to use Println() function
// to print the message on the console screen
import "fmt"
func main() {
log.Panic("Warning: logging data")
fmt.Println("Program finished")
}
Output:
2021/04/22 14:00:04 Warning: logging data
panic: Warning: logging data
goroutine 1 [running]:
log.Panic(0xc00002c768, 0x1, 0x1)
/usr/local/go/src/log/log.go:333 +0xac
main.main()
/home/main.go:15 +0x61
Program exited: status 2.
Explanation:
In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the "fmt" package to use the Println() function and we also imported the "log" package to use the log.Panic() function.
In the main() function, we used log.Panic() function to print specified message with timestamp on the console screen. Here, we printed a warning message on the console screen.
The log.Panic() is similar to the log.Print() function followed by a call to Panic() function.
The log.Panic() function calls Panic() function that's why, in above program fmt.Println() function did not call after log.Panic() function.
Golang log Package Programs »