added flags!

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

@ -2,7 +2,11 @@ package main
import ( import (
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"io/ioutil"
"log"
"net/http"
"os" "os"
) )
@ -67,49 +71,33 @@ type Sys struct {
} }
func main() { func main() {
/* //set up the flags for the program to use!
//setting up openweathermap api var city, code string
lat := "56.122970" flag.StringVar(&city, "city", "London", "Enter city e.g. London, Glasgow etc...")
lon := "-3.932390" flag.StringVar(&code, "code", "uk", "Enter Code e.g. uk, us etc...")
//city := "Stirling, uk"
api_key := "29e5139b86638988d333a28ba360bfd9" flag.Parse()
full := "https://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&appid=" + api_key + "" //setting up openweathermap api
fmt.Printf("\n------\nWeather url: %+s\n---------\n", full) api_key := "29e5139b86638988d333a28ba360bfd9"
full := "https://api.openweathermap.org/data/2.5/weather?q=" + city + "," + code + "&units=metric" + "&appid=" + api_key + ""
//read webpage fmt.Printf("\n------\nWeather url: %+s\n---------\n", full)
url := full
resp, err := http.Get(url) //read webpage
//reads html as a slice of bytes url := full
html, err := ioutil.ReadAll(resp.Body) resp, err := http.Get(url)
if err != nil { //reads html as a slice of bytes
panic(err) html, err := ioutil.ReadAll(resp.Body)
} if err != nil {
//print the html panic(err)
//fmt.Printf("%s\n", html) }
//print the html
*/ fmt.Printf("%s\n", html)
/* fmt.Println("---------- JSON PARSED -----------\n\n")
jsonFile, err := os.Open("stirling.json")
// if we os.Open returns an error then handle it //output the html to stirling.json
if err != nil { CreateFile(string(html))
fmt.Println(err)
} data, _ := LoadConfiguration("weather.json")
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")
fmt.Println(data) fmt.Println(data)
} }
func LoadConfiguration(file string) (WeatherAPI, error) { func LoadConfiguration(file string) (WeatherAPI, error) {
@ -122,3 +110,39 @@ func LoadConfiguration(file string) (WeatherAPI, error) {
jsonParser.Decode(&config) jsonParser.Decode(&config)
return config, err 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