package main import ( "log" ) func main() { log.Println("Hello, world!") }
package main import ( "log" "os" ) func main() { f, err := os.Create("logfile.txt") if err != nil { log.Fatal("Unable to create log file:", err) } defer f.Close() log.SetOutput(f) log.Println("This message will be written to a log file") }In this example, we create a log file and set it as the output for the standard logger using the SetOutput method. The message "This message will be written to a log file" is logged to the file instead of the console. Both examples use the log package library provided by Go.