time.LoadLocation() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The LoadLocation() function in Go language is used to find a location with the name stated. So, if the stated name is “UTC” then it returns UTC and if the stated name is “Local” then it returns Local. Else, the name to be used is assumed to be a location which is equivalent to a file in the database of IANA Time Zone. Where this database is present only on Unix systems. Moreover, this function is defined under the time package. Here, you need to import the “time” package in order to use these functions.
Syntax:
func LoadLocation(name string) (*Location, error)
Here, “name” is the name of the location to be used, and *Location is the pointer to the Location. Where “Location” forms the set of time offsets in use. And “error” is a panic error.
Return Value: It returns the location with the stated name.
Example 1:
package main
import (
"fmt"
"time"
)
func main() {
locat, error := time .LoadLocation( "Asia/Kolkata" )
if error != nil {
panic(error)
}
fmt.Println(locat)
}
|
Output:
Asia/Kolkata
Here, IANA time zone of India is returned as there is no error.
Example 2:
package main
import (
"fmt"
"time"
)
func main() {
locat, error := time .LoadLocation( "Asia/Kolkata" )
if error != nil {
panic(error)
}
tm := time .Date(2020, 4, 7, 16,
7, 0, 0, time .UTC)
fmt.Println( tm .In(locat))
}
|
Output:
2020-04-07 21:37:00 +0530 IST
Here, at first LoadLocation() method is called then the Date() method is called with its parameters i.e, date and time then the date and time of the stated location are returned.
Last Updated :
21 Apr, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...