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/recipe06.
  2. Navigate to the directory.
  3. Create the arithmetics.go file with the following content:
        package main        import (          "fmt"          "time"        )        func main() {          l, err := time.LoadLocation("Europe/Vienna")          if err != nil {            panic(err)          }          t := time.Date(2017, 11, 30, 11, 10, 20, 0, l)          fmt.Printf("Default date is: %v\n", t)          // Add 3 days          r1 := t.Add(72 * time.Hour)          fmt.Printf("Default date +3HRS is: %v\n", r1)          // Subtract 3 days          r1 = t.Add(-72 * time.Hour)          fmt.Printf("Default date -3HRS is: %v\n", r1)          // More comfortable api          // to add days/months/years          r1 = t.AddDate(1, 3, 2)          fmt.Printf("Default date +1YR +3MTH +2D is: %v\n", r1)        }
  1. Execute the code by running go run arithmetics.go in the main ...

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