在Go語言中,時間包提供了確定和查看時間的函數。 Go語言中的LoadLocation()函數用於查找具有指定名稱的位置。因此,如果聲明的名稱為“UTC”,則返回UTC;如果聲明的名稱為“Local”,則返回Local。否則,假定要使用的名稱是一個等效於IANA時區數據庫中文件的位置。僅在Unix係統上存在此數據庫的地方。此外,此函數在時間包下定義。在這裏,您需要導入“time”包才能使用這些函數。
用法:
func LoadLocation(name string) (*Location, error)
此處,“name”是要使用的位置的名稱,* Location是指向該位置的指針。其中“Location”構成了使用中的時間偏移集。 “error”是一個緊急錯誤。
返回值:它返回具有指定名稱的位置。
範例1:
// Golang program to illustrate the usage of
// LoadLocation() 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)
}
// Prints location
fmt.Println(locat)
}
輸出:
Asia/Kolkata
在此,返回印度的IANA時區,因為沒有錯誤。
範例2:
// Golang program to illustrate the usage of
// LoadLocation() 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 Date() method
// with its parameter
tm := time.Date(2020, 4, 7, 16,
7, 0, 0, time.UTC)
// Prints the time and date
// of the stated location
fmt.Println(tm.In(locat))
}
輸出:
2020-04-07 21:37:00 +0530 IST
在這裏,首先調用LoadLocation()方法,然後調用Date()方法及其參數,即日期和時間,然後返回指定位置的日期和時間。
相關用法
- Golang math.Lgamma()用法及代碼示例
- Golang math.Float64bits()用法及代碼示例
- Golang atomic.AddInt64()用法及代碼示例
- Golang atomic.StoreInt64()用法及代碼示例
- Golang reflect.FieldByIndex()用法及代碼示例
- Golang string.Contains用法及代碼示例
- Golang bits.Sub()用法及代碼示例
- Golang io.PipeWriter.CloseWithError()用法及代碼示例
- Golang time.Round()用法及代碼示例
- Golang reflect.AppendSlice()用法及代碼示例
- Golang reflect.ChanOf()用法及代碼示例
- Golang flag.Bool()用法及代碼示例
- Golang time.Sleep()用法及代碼示例
- Golang time.Time.Year()用法及代碼示例
- Golang reflect.DeepEqual()用法及代碼示例
- Golang reflect.Indirect()用法及代碼示例
- Golang reflect.CanAddr()用法及代碼示例
- Golang reflect.CanInterface()用法及代碼示例
- Golang reflect.CanSet()用法及代碼示例
- Golang reflect.Cap()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 time.LoadLocation() Function in Golang With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。