Home »
Golang »
Golang Programs
Golang program to demonstrate the log.Printf() function
Here, we are going to demonstrate the log.Printf() function in Golang (Go Language).
Submitted by Nidhi, on April 22, 2021 [Last updated : March 05, 2023]
log.Printf() function in Golang
Problem Solution:
In this program, we will use a log.Printf() function to print formatted messages with timestamp on the console screen.
Program/Source Code:
The source code to demonstrate the log.Printf() 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.Printf() function
// Golang program to demonstrate the
// log.Printf() function
package main
// Import log package to use
// Printf() 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() {
var count int = 5
log.Printf("Counter : %d", count)
fmt.Println("Program finished")
}
Output:
2021/04/22 13:33:45 Counter : 5
Program finished
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.Printf() function.
In the main() function, we used log.Printf() function to print formatted messages with timestamp on the console screen.
Golang log Package Programs »