×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang program to demonstrate the log.Fatal() function

Here, we are going to demonstrate the log.Fatal() function in Golang (Go Language).
Submitted by Nidhi, on April 22, 2021 [Last updated : March 05, 2023]

log.Fatal() function in Golang

Problem Solution:

In this program, we will use log.Fatal() function to print specified message with timestamp on the console screen. The log.Fatal() is similar to the log.Print() function followed by a call to os.Exit(1) function.

Program/Source Code:

The source code to demonstrate the log.Fatal() 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.Fatal() function

// Golang program to demonstrate the
// log.Fatal() function

package main

// Import log package to use
// Fatal() function to print log
import "log"

// Import fmt package to use Println() function
// to print the message on the console screen
import "fmt"

func main() {
	log.Fatal("Data log")
	fmt.Println("Program finished")
}

Output:

2021/04/22 13:42:23 Data log
Program exited: status 1.

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.Fatal() function.

In the main() function, we used log.Fatal() function to print specified message with timestamp on the console screen. The log.Fatal() is similar to the log.Print() function followed by a call to os.Exit(1) function.

The log.Fatal() calls os.Exit(1) that's why, in above program fmt.Println() function did not call after log.Fatal() function.

Golang log Package Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.