started cleaning the code up!

master
Simon Kellet 2 years ago
parent e53081277d
commit 421bc921a9
  1. 51
      main.go

@ -51,7 +51,7 @@ type Main struct {
type Wind struct { type Wind struct {
Speed float64 `json:"speed"` Speed float64 `json:"speed"`
Deg float64 `json:"deg"` Deg float64 `json:"deg"`
gust float64 `json:"gust"` Gust float64 `json:"gust"`
} }
type Rain struct { type Rain struct {
@ -72,15 +72,13 @@ type Sys struct {
func main() { func main() {
//set up the flags for the program to use! //set up the flags for the program to use!
var city, code string var city, code, api_key string
flag.StringVar(&city, "city", "London", "Enter city e.g. London, Glasgow etc...") flag.StringVar(&city, "city", "London", "Enter city e.g. London, Glasgow etc... (Default: London)")
flag.StringVar(&code, "code", "uk", "Enter Code e.g. uk, us etc...") flag.StringVar(&code, "code", "uk", "Enter Code e.g. uk, us etc... (Default: uk)")
flag.StringVar(&code, "api", "none", "Enter Code e.g. uk, us etc... Default(none)")
flag.Parse() 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 + "" 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 //read webpage
url := full url := full
@ -90,16 +88,13 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
//print the html //Create file
fmt.Printf("%s\n", html) CreateFile(string(html), false)
fmt.Println("---------- JSON PARSED -----------\n\n")
//output the html to stirling.json
CreateFile(string(html))
data, _ := LoadConfiguration("weather.json") data, _ := LoadConfiguration("weather.json")
fmt.Println(data) fmt.Println(data)
} }
func LoadConfiguration(file string) (WeatherAPI, error) { func LoadConfiguration(file string) (WeatherAPI, error) {
var config WeatherAPI var config WeatherAPI
configFile, err := os.Open(file) configFile, err := os.Open(file)
@ -110,39 +105,25 @@ 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 func CreateFile(fileContents string, debugFlag bool) {
// 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 fmt.Printf("Writing to a file in Go lang\n")
// by the err variable and Fatalf method of
// log prints the error message and stops
// program execution
file, err := os.Create("weather.json") file, err := os.Create("weather.json")
if err != nil { if err != nil {
log.Fatalf("failed creating file: %s", err) 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() defer file.Close()
// len variable captures the length
// of the string written to the file.
len, err := file.WriteString(fileContents) len, err := file.WriteString(fileContents)
if err != nil { if err != nil {
log.Fatalf("failed writing to file: %s", err) log.Fatalf("failed writing to file: %s", err)
} }
// Name() method returns the name of the // Debug Flag
// file as presented to Create() method. if debugFlag {
fmt.Printf("\nFile Name: %s", file.Name()) fmt.Printf("\nFile Name: %s", file.Name())
fmt.Printf("\nLength: %d bytes", len) fmt.Printf("\nLength: %d bytes", len)
}
} }

Loading…
Cancel
Save