The source code to set date and time flags using log.SetFlags() for log messages is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.
// Golang program to set date and time flags
// using log.SetFlags() for log messages
package main
import "log"
import "fmt"
func main() {
log.SetFlags(log.Ldate)
log.Println("Log line1")
log.SetFlags(log.Ltime)
log.Println("Log line2")
log.SetFlags(log.Lmicroseconds)
log.Println("Log line3")
log.SetFlags(log.Ltime | log.LUTC)
log.Println("Log line4")
fmt.Println("Program finished")
}
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 functions.
In the main() function, we set date and time flags using log.SetFlags() function. The log.SetFlags() function is used to set some prefix value to the log messages.
Program/Source Code:
The source code to set date and time flags using log.SetFlags() for log messages is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.
Output:
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 functions.
In the main() function, we set date and time flags using log.SetFlags() function. The log.SetFlags() function is used to set some prefix value to the log messages.
need an explanation for this answer? contact us directly to get an explanation for this answer