got the structs working!

master
Simon Kellet 2 years ago
parent 63006015a6
commit 1993a207de
  1. 117
      main.go

@ -1,24 +1,69 @@
package main
import (
"encoding/json"
"fmt"
"os"
)
type WeatherAPI struct {
Coord Coord `json:"coord"`
Weather Weather `json:"weather`
Base string `json:"base"`
Main Main `json:"main"`
Visability string `json:"visibility"`
Wind Wind `json:"wind"`
Rain Rain `json:"rain"`
Clouds Cloud `json:"cloud"`
dt DT `json:"dt"`
Sys Sys `json:"sys"`
TimeZone int `json:"timezone"`
ID int `json:"id"`
Name string `json:"name"`
COD int `json:"cod"`
Coord Coord `json:"coord"`
Weather []Weather `json:"weather"`
Base string `json:"base"`
Main Main `json:"main"`
Visability int64 `json:"visibility"`
Wind Wind `json:"wind"`
Rain Rain `json:"rain"`
Clouds Cloud `json:"cloud"`
DT int64 `json:"dt"`
Sys Sys `json:"sys"`
TimeZone int `json:"timezone"`
ID int `json:"id"`
Name string `json:"name"`
COD int `json:"cod"`
}
type Coord struct {
//etc
Lon float64 `json:"lon"`
Lat float64 `json:"lat"`
}
type Weather struct {
ID int `json:"id"`
Main string `json:"main"`
Description string `json:"description"`
Icon string `json:"icon"`
}
type Main struct {
Temp float64 `json:"temp"`
Feels_like float64 `json:"feels_like"`
Temp_min float64 `json:"temp_min"`
Temp_max float64 `json:"temp_max"`
Pressure float64 `json:"pressure"`
Humidity float64 `json:"humidity"`
}
type Wind struct {
Speed float64 `json:"speed"`
Deg float64 `json:"deg"`
gust float64 `json:"gust"`
}
type Rain struct {
T1h float64 `json:"1h"`
}
type Cloud struct {
All int `json:"all"`
}
type Sys struct {
Types int `json:"type"`
Id int64 `json:"id"`
Country string `json:"country"`
Sunrise int64 `json:"sunrise"`
Sunset int64 `json:"sunset"`
}
func main() {
@ -26,7 +71,7 @@ func main() {
//setting up openweathermap api
lat := "56.122970"
lon := "-3.932390"
city := "Stirling, uk"
//city := "Stirling, uk"
api_key := "29e5139b86638988d333a28ba360bfd9"
full := "https://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&appid=" + api_key + ""
fmt.Printf("\n------\nWeather url: %+s\n---------\n", full)
@ -39,19 +84,41 @@ func main() {
if err != nil {
panic(err)
}
*/
//print the html
//fmt.Printf("%s\n", html)
/*
out, _ := json.Marshal(html)
fmt.Printf("%s\n", out)
//print the html
//fmt.Printf("%s\n", html)
*/
/*
byteArray, error := json.MarshalIndent(html, "", " ")
if error != nil {
jsonFile, err := os.Open("stirling.json")
// if we os.Open returns an error then handle it
if err != nil {
fmt.Println(err)
}
fmt.Println("Successfully Opened stirling.json")
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
fmt.Println(string(byteArray))
var data WeatherAPI
error := json.Unmarshal([]byte(byteValue), &data)
if error != nil {
fmt.Println(error)
return
}
//json.Unmarshal(byteValue, &data)
fmt.Println(data)
*/
data, _ := LoadConfiguration("./stirling.json")
fmt.Println(data)
}
func LoadConfiguration(file string) (WeatherAPI, error) {
var config WeatherAPI
configFile, err := os.Open(file)
if err != nil {
fmt.Println(err.Error())
}
jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&config)
return config, err
}

Loading…
Cancel
Save