time.Location.String() Function in Golang With Examples
Last Updated :
21 Apr, 2020
Improve
In Go language, time packages supplies functionality for determining as well as viewing time. The Location.String() function in Go language is used to find an explanatory name stated for the data of time zone which is equivalent to the name parameter passes to the LoadLocation or FixedZone method. Moreover, this function is defined under the time package. Here, you need to import "time" package in order to use these functions.
Syntax:
C
Output:
C
Output:
func (l *Location) String() stringHere, "l" 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. Return Value: It returns an explanatory name stated for the data of time zone. Example 1:
// Golang program to illustrate the usage of
// Location.String() function
// Including main package
package main
// Importing fmt and time
import (
"fmt"
"time"
)
// Calling main
func main() {
// Calling LoadLocation
// method with its parameter
locat, error := time.LoadLocation("Asia/Kolkata")
// If error not
// equal to nil then
// return panic error
if error != nil {
panic(error)
}
// Calling Location.String()
// method and printing
// location name
fmt.Println(locat.String())
}
Asia/KolkataHere, the IANA time zone of India is returned as there is no error. Example 2:
// Golang program to illustrate the usage of
// Location.String() function
// Including main package
package main
// Importing fmt and time
import (
"fmt"
"time"
)
// Calling main
func main() {
// Calling FixedZone method
// with its parameter
location := time.FixedZone("UTC-7", -7*50*50)
// Calling Location.String()
// method and printing
// the stated location
fmt.Println(location.String())
}
UTC-7