test
This commit is contained in:
parent
9cef9579e3
commit
f1fca715fb
8
file.go
8
file.go
@ -5,16 +5,16 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func createFile(fileContents string) {
|
||||
file, err := os.Create("weather.json")
|
||||
func createFile(fileContents string, filename string) {
|
||||
file, err := os.Create(filename)
|
||||
if err != nil {
|
||||
log.Fatalf("failed creating file: %s", err)
|
||||
log.Fatalf("failed creating file %s: %s", filename, err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = file.WriteString(fileContents)
|
||||
if err != nil {
|
||||
log.Fatalf("failed writing to file: %s", err)
|
||||
log.Fatalf("failed writing to file %s: %s", filename, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
9
main.go
9
main.go
@ -16,6 +16,9 @@ func main() {
|
||||
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
|
||||
@ -27,7 +30,8 @@ func main() {
|
||||
}
|
||||
|
||||
//Create file
|
||||
createFile(string(html))
|
||||
createFile(string(html), filename)
|
||||
defer removeFile(filename) //delete file at the end
|
||||
|
||||
//Load the file
|
||||
data, err := loadJSON("weather.json")
|
||||
@ -38,7 +42,4 @@ func main() {
|
||||
|
||||
//Print the weather!
|
||||
printWeather(data)
|
||||
|
||||
//remove the file at the end
|
||||
removeFile("weather.json")
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user