package main import ( "encoding/json" "errors" "fmt" "os" "time" "git.red-green.com/david/astruct" ) func main() { var err error _, err = os.Stat("output.go") if err != nil { if !errors.Is(err, os.ErrNotExist) { fmt.Println("Err:", err) return } var a *astruct.Astruct = astruct.NewAstruct("Weather") err = a.ReadFile("test.json") if err != nil { fmt.Println("Err:", err) return } fmt.Println("MapLikes:") for name, m := range a.MapLikes { if name == "" { continue } fmt.Printf("%s\n", name) for k, v := range m { fmt.Printf(" '%s' = %s\n", k, v.String()) } } fmt.Println("ArrayLikes:") for name, a := range a.ArrayLikes { if name == "" { continue } fmt.Printf("%s\n", name) for i, v := range a { fmt.Printf(" %d = %s\n", i, v.String()) } } err = a.WriteFile("output.go", "main", 0666) if err != nil { fmt.Println("Err:", err) return } } // --- Test unmarshaling our new go code --- var ( pay []byte data Weather ) pay, err = os.ReadFile("test.json") if err != nil { fmt.Println("Err:", err) return } err = json.Unmarshal(pay, &data) if err != nil { fmt.Println("Err:", err) return } fmt.Println("Weather Report:") fmt.Printf("%s, %s\n", data.Name, data.Sys.Country) fmt.Printf("lon %0.4f, lat %0.4f\n", data.Coord.Lon, data.Coord.Lat) fmt.Printf("Currently %s with %0.0f%% cload coverage\n", data.Weather[0].Description, data.Clouds.All) fmt.Printf("Wind of %0.1f MPH with gusts of upto %0.1f MPH\n", data.Wind.Speed, data.Wind.Gust) fmt.Printf("It feels like %0.1f F, Low is %0.1f F with a high of %0.1f F\n", data.Main.Feels_like, data.Main.Temp_min, data.Main.Temp_max) fmt.Printf("Humidity of %0.0f%%\n", data.Main.Humidity) at := time.Unix(data.Dt, 0).Round(time.Second) fmt.Printf("This report was generated on %s\n", at.Format(time.UnixDate)) rise := time.Unix(data.Sys.Sunrise, 0).Round(time.Second) set := time.Unix(data.Sys.Sunset, 0).Round(time.Second) fmt.Printf("Sun rise at %s\n", rise.Format("03:04:05PM")) fmt.Printf("Sun set at %s\n", set.Format("03:04:05PM")) }