Finding today's date
Obtaining the current date is a very common task for any system or application. Let's look at how this is done with help of Go's standard library.
How to do it...
- Open the console and create the folder
chapter04/recipe01
. - Navigate to the directory.
- Create the
today.go
file with the following content:
package main import ( "fmt" "time" ) func main() { today := time.Now() fmt.Println(today) }
- Execute the code by running
go run today.go
in the main Terminal. - You will see the following output:

How it works...
The built-in package time
contains the function Now
, which provides the instance of a Time
initialized to the current local time and date.
The Time
type is an instant in time in nanoseconds. The zero value of Time
is January 1, year 1, 00:00:00.000000000 UTC.
Note
The pointer to the Time
type should not be used. If only the value (not pointer to variable) is used, the Time
instance is considered to...