Golang time.Location.String()函数及示例

Golang time.Location.String()函数及示例

Go 语言中,time 包提供了确定和查看时间的功能。 Location.String() 函数用于查找时间区域的数据的说明性名称,该名称等效于 LoadLocation 或 FixedZone 方法中传递的 name 参数。此外,该函数在 time 包中定义。在这里,您需要导入“time”包以使用这些函数。

语法:

func (l *Location) String() string

这里,“l”是要使用的位置的名称,*Location 是指向 Location 的指针。其中,“Location”形成正在使用的时间偏移量的集合。

返回值:它返回时间区域数据的说明性名称。

示例1:

// Golang 演示在使用时Location.String()函数的用法
  
// 包含主包
包 main
  
// 导入fmt和time
导入 (
    "fmt"
    "time"
)
  
// 调用主函数
func main() {
  
    // 使用 LoadLocation 调用方法 
    // 它的参数
    locat,error := time.LoadLocation("Asia/Kolkata")
  
    // 如果错误不相等于 nil,则
    // 返回 panic 错误
    if error != nil {
        panic(error)
    }
  
    // 调用 Location.String()
    // 方法并打印位置名称
    fmt.Println(locat.String())
} 

输出:

Asia/Kolkata

这里,如果没有错误,则返回印度的 IANA 时区。

示例2:

// Golang 演示在使用时Location.String()函数的用法
  
// 包含主包
包 main
  
// 导入fmt和time
导入 (
    "fmt"
    "time"
)
  
// 调用主函数
func main() {
  
    // 调用 FixedZone 方法
    // 它的参数
    location := time.FixedZone("UTC-7", -7*50*50)
  
    // 调用Location.String()
    // 方法并打印所述位置
    fmt.Println(location.String())
} 

输出:

UTC-7

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程