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

46 lines
1.0 KiB

3 years ago
package main
import (
3 years ago
"flag"
"fmt"
"os"
)
2 years ago
//TODO: finish this!
3 years ago
func main() {
3 years ago
//set up the flags for the program to use!
2 years ago
var city, code, apiKey string
3 years ago
flag.StringVar(&city, "city", "London", "Enter city e.g. London")
flag.StringVar(&code, "code", "uk", "Enter Code e.g. uk")
2 years ago
flag.StringVar(&apiKey, "api", "none", "Enter you api key here")
3 years ago
flag.Parse()
2 years ago
var filename string
filename = "weather.json"
2 years ago
full := "https://api.openweathermap.org/data/2.5/weather?q=" + city + "," + code + "&units=metric" + "&appid=" + apiKey + ""
3 years ago
//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)
}
3 years ago
//Create file
2 years ago
createFile(string(html), filename)
defer removeFile(filename) //delete file at the end
//Load the file
2 years ago
data, err := loadJSON("weather.json")
3 years ago
if err != nil {
fmt.Println("Cannot load weather.json file!")
2 years ago
os.Exit(1)
}
//Print the weather!
printWeather(data)
3 years ago
}