Open In App

time.Location.String() Function in Golang With Examples

Last Updated : 21 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:

func (l *Location) String() string

Here, “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())
}


Output:

Asia/Kolkata

Here, 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())
}


Output:

UTC-7


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads