Home > Backend Development > Golang > How Can I Efficiently Populate a Go Array with Available Time Zones for Formatting?

How Can I Efficiently Populate a Go Array with Available Time Zones for Formatting?

Barbara Streisand
Release: 2024-11-30 21:02:18
Original
844 people have browsed it

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"
)
Copy after login

Declare directory paths for diverse operating systems:

var zoneDirs = []string{
    "/usr/share/zoneinfo/",
    "/usr/share/lib/zoneinfo/",
    "/usr/lib/locale/TZ/",
}
Copy after login

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:])
        }
    }
}
Copy after login

Execute the program to retrieve a comprehensive list of time zones:

func main() {
    for _, zoneDir = range zoneDirs {
        ReadFile("")
    }
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template