import ( "fmt" "time" ) func main() { duration := 5 * time.Second fmt.Println(duration) }
5s
import ( "fmt" "time" ) func main() { durationStr := "2h30m" duration, err := time.ParseDuration(durationStr) if err != nil { fmt.Println("Error parsing duration:", err) return } fmt.Println(duration) }
2h30m0s
import ( "fmt" "time" ) func main() { now := time.Now() duration := 1 * time.Hour futureTime := now.Add(duration) fmt.Println("Now:", now) fmt.Println("Future time:", futureTime) }
Now: 2021-10-26 10:15:15.350538 +0530 IST m=+0.000043152 Future time: 2021-10-26 11:15:15.350538 +0530 IST m=+3600.000043152Description: In this example, we create a timestamp using the time.Now() function. We also create a duration of 1 hour using the time.Duration type. We then add the duration to the timestamp using the time.Add() function, and print out both the original timestamp and the new timestamp.