Web Development

Getting the current date is a familiar task for most programmers. Here’s how to get the current time in Go Lang.

Go Lang Current Time Code

package main

import (
   "fmt"
   "time"
)

func main() {
    current_time := time.Now().Local()
    fmt.Println("The Current time is ", current_time.Format("2006-01-02"))
}

Output

$ go run get_current_time.go
The Current time is  2015-11-13

Go Lang Current Time Code in UTC

If you want to use UTC instead of your local time, you can do the following:

package main

import (
   "fmt"
   "time"
)

func main() {
    current_time := time.Now().UTC()
    fmt.Println("The Current time is ", current_time.Format("2006-01-02 MST"))

}

Output

$ go run get_current_time.go
The Current time is  2015-11-13 UTC

Notes:

  • time.Format should use the layout, Mon Jan 2 15:04:05 MST 2006 to show the pattern