format a date in to the format 'yyyy-mm-dd hh:mm:ss.fff' in go

You can use the time package in Go to format a date into the desired format. Here's an example:

main.go
package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Now()
    fmt.Println(t.Format("2006-01-02 15:04:05.000"))
}
136 chars
12 lines

In this example, we're getting the current time using time.Now() and then formatting it using the Format method with the layout string "2006-01-02 15:04:05.000". The layout string uses specific placeholders for different parts of the date and time, such as 2006 for the year, 01 for the month, 02 for the day, 15 for the hour in 24-hour format, 04 for the minute, 05 for the second, and 000 for the microsecond.

related categories

gistlibby LogSnag