You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-weather/main.go

125 lines
2.7 KiB

2 years ago
package main
import (
"encoding/json"
"fmt"
"os"
)
2 years ago
type WeatherAPI struct {
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"`
2 years ago
}
type Coord struct {
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"`
2 years ago
}
func main() {
/*
//setting up openweathermap api
lat := "56.122970"
lon := "-3.932390"
//city := "Stirling, uk"
2 years ago
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)
//read webpage
url := full
resp, err := http.Get(url)
//reads html as a slice of bytes
html, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
//print the html
//fmt.Printf("%s\n", html)
2 years ago
*/
/*
jsonFile, err := os.Open("stirling.json")
// if we os.Open returns an error then handle it
if err != nil {
2 years ago
fmt.Println(err)
}
fmt.Println("Successfully Opened stirling.json")
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
2 years ago
var data WeatherAPI
error := json.Unmarshal([]byte(byteValue), &data)
if error != nil {
fmt.Println(error)
return
}
//json.Unmarshal(byteValue, &data)
fmt.Println(data)
2 years ago
*/
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
2 years ago
}