當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Golang time.Location.String()用法及代碼示例

在Go語言中,時間包提供了確定和查看時間的函數。 Go語言中的Location.String()函數用於查找時區數據的解釋性名稱,該名稱與傳遞給LoadLocation或FixedZone方法的名稱參數等效。此外,此函數在時間包下定義。在這裏,您需要導入“time”軟件包才能使用這些函數。

用法:

func (l *Location) String() string

此處,“l”是要使用的位置的名稱,* Location是指向該位置的指針。其中,“Location”形成了使用中的時間偏移量集。

返回值:它返回為時區數據說明的解釋性名稱。

範例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/Kolkata

在此,返回印度的IANA時區,因為沒有錯誤。

範例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



相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 time.Location.String() Function in Golang With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。