time.Truncate() Function in Golang With Examples
In Go language, time packages supply functionality for determining as well as viewing time. The Truncate() function in Go language is used to find the outcome of rounding the stated duration ‘d’ towards zero to a multiple of ‘m’ duration. Moreover, this function is defined under the time package. Here, you need to import the “time” package in order to use these functions.
func (d Duration) Truncate(m Duration) Duration
Here, d is the duration of time that will be rounded and m is the multiple.
Return Value: It returns the outcome of rounding the stated duration ‘d’ towards zero to a multiple of ‘m’ duration. But if m is less than or equal to zero then it returns unaltered ‘d’.
Truncated duration is : 45m32s
Here, ‘d’ is rounded to the multiple of m.
Truncated(1µs) is : 7m11.053077s Truncated(1s) is : 7m11s Truncated(4s) is : 7m8s Truncated(11m0s) is : 0s
Here, an array of ‘t’ is formed first then a range is used in order to iterate over all the values of duration ‘t’. And at last Truncate() method is used to print all the rounded values of t in the above code.