added flags!

master
Simon Kellet 2 years ago
parent 1993a207de
commit e53081277d
  1. 110
      main.go

@ -2,7 +2,11 @@ package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
@ -67,49 +71,33 @@ type Sys struct {
}
func main() {
/*
//setting up openweathermap api
lat := "56.122970"
lon := "-3.932390"
//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)
//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)
*/
/*
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)
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")
//set up the flags for the program to use!
var city, code string
flag.StringVar(&city, "city", "London", "Enter city e.g. London, Glasgow etc...")
flag.StringVar(&code, "code", "uk", "Enter Code e.g. uk, us etc...")
flag.Parse()
//setting up openweathermap api
api_key := "29e5139b86638988d333a28ba360bfd9"
full := "https://api.openweathermap.org/data/2.5/weather?q=" + city + "," + code + "&units=metric" + "&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)
fmt.Println("---------- JSON PARSED -----------\n\n")
//output the html to stirling.json
CreateFile(string(html))
data, _ := LoadConfiguration("weather.json")
fmt.Println(data)
}
func LoadConfiguration(file string) (WeatherAPI, error) {
@ -122,3 +110,39 @@ func LoadConfiguration(file string) (WeatherAPI, error) {
jsonParser.Decode(&config)
return config, err
}
func CreateFile(fileContents string) {
// fmt package implements formatted
// I/O and has functions like Printf
// and Scanf
fmt.Printf("Writing to a file in Go lang\n")
// in case an error is thrown it is received
// by the err variable and Fatalf method of
// log prints the error message and stops
// program execution
file, err := os.Create("weather.json")
if err != nil {
log.Fatalf("failed creating file: %s", err)
}
// Defer is used for purposes of cleanup like
// closing a running file after the file has
// been written and main //function has
// completed execution
defer file.Close()
// len variable captures the length
// of the string written to the file.
len, err := file.WriteString(fileContents)
if err != nil {
log.Fatalf("failed writing to file: %s", err)
}
// Name() method returns the name of the
// file as presented to Create() method.
fmt.Printf("\nFile Name: %s", file.Name())
fmt.Printf("\nLength: %d bytes", len)
}

Loading…
Cancel
Save