O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open the console and create the folder chapter04/recipe04.
  2. Navigate to the directory.
  3. Create the epoch.go file with the following content:
        package main        import (          "fmt"          "time"        )        func main() {          // Set the epoch from int64          t := time.Unix(0, 0)          fmt.Println(t)          // Get the epoch          // from Time instance          epoch := t.Unix()          fmt.Println(epoch)          // Current epoch time          apochNow := time.Now().Unix()          fmt.Printf("Epoch time in seconds: %d\n", apochNow)          apochNano := time.Now().UnixNano()          fmt.Printf("Epoch time in nano-seconds: %d\n", apochNano)        }
  1. Execute the code by running go run epoch.go in the main Terminal.
  2. You will see the following output:

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required