Home >Backend Development >Golang >How Can I Efficiently Populate a Go Array with Available Time Zones for Formatting?
Explore Time Zones in Go: Populating an Array for Time Formatting
Considering the need to populate an array with accepted time zones for time formatting in Go, let's delve into a resourceful approach:
To procure a list of time zones, initiate the following steps:
import ( "fmt" "os" "strings" )
Declare directory paths for diverse operating systems:
var zoneDirs = []string{ "/usr/share/zoneinfo/", "/usr/share/lib/zoneinfo/", "/usr/lib/locale/TZ/", }
Function to recursively read directories and print valid time zones:
func ReadFile(path string) { files, _ := os.ReadDir(zoneDir + path) for _, f := range files { if f.Name() != strings.ToUpper(f.Name()[:1]) + f.Name()[1:] { continue } if f.IsDir() { ReadFile(path + "/" + f.Name()) } else { fmt.Println((path + "/" + f.Name())[1:]) } } }
Execute the program to retrieve a comprehensive list of time zones:
func main() { for _, zoneDir = range zoneDirs { ReadFile("") } }
This robust technique provides a comprehensive list of time zones that can be effortlessly integrated into HTML templates, empowering users to select and display time in their preferred zone.
The above is the detailed content of How Can I Efficiently Populate a Go Array with Available Time Zones for Formatting?. For more information, please follow other related articles on the PHP Chinese website!