Consuming Time JSON in Go
Go is strict about types. Our API returns a consistent schema, making it easy to unmarshal into Go structs for robust system tools and backend services.
GO
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type TimeResponse struct {
Datetime string `json:"datetime"`
Timezone string `json:"timezone"`
UnixTime int64 `json:"unixtime"`
}
func main() {
resp, err := http.Get("https://time.now/developer/api/timezone/Europe/Berlin")
if err != nil {
panic(err)
}
defer resp.Body.Close()
var record TimeResponse
if err := json.NewDecoder(resp.Body).Decode(&record); err != nil {
panic(err)
}
fmt.Printf("Time in %s is %s\n", record.Timezone, record.Datetime)
}
Why use Time.Now for Backend?
- Strict JSON Schema
- Fast execution for system tools
- Easy struct mapping