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

45 lines
1.0 KiB

package main
import (
"flag"
"fmt"
"os"
)
//TODO: finish this!
func main() {
//set up the flags for the program to use!
var city, code, apiKey string
flag.StringVar(&city, "city", "London", "Enter city e.g. London")
flag.StringVar(&code, "code", "uk", "Enter Code e.g. uk")
flag.StringVar(&apiKey, "api", "none", "Enter you api key here")
flag.Parse()
var filename string
filename = "weather.json"
full := "https://api.openweathermap.org/data/2.5/weather?q=" + city + "," + code + "&units=metric" + "&appid=" + apiKey + ""
//read webpage
html, err := loadWebPage(full)
if err != nil {
//fmt.Println("4xx error, please try again!")
fmt.Fprintf(os.Stderr, "ERROR: Could not read website: %s\n", err)
os.Exit(1)
}
//Create file
createFile(string(html), filename)
defer removeFile(filename) //delete file at the end
//Load the file
data, err := loadJSON("weather.json")
if err != nil {
fmt.Println("Cannot load weather.json file!")
os.Exit(1)
}
//Print the weather!
printWeather(data)
}