get today's date in go

To get today's date in Go, you can use the time.Now() function to get the current time and date, and then use the Format() method to format the output as a date string.

Here's an example code snippet:

main.go
package main

import (
    "fmt"
    "time"
)

func main() {
    today := time.Now().Format("2006-01-02")
    fmt.Println(today)
}
131 chars
12 lines

In this example, we use the Format() method to format the current time and date as a string in the format YYYY-MM-DD. The format string uses specific placeholders to specify the year, month, and day components:

  • 2006: represents the year
  • 01: represents the month
  • 02: represents the day

This code will output the current date in the format YYYY-MM-DD.

gistlibby LogSnag