32 lines
576 B
Go
32 lines
576 B
Go
package weather
|
|
|
|
import (
|
|
"demo/weather/geo"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
func GetWeather(geo geo.GeoData, format int) string {
|
|
baseUrl, err := url.Parse("https://wttr.in/" + geo.City)
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
return ""
|
|
}
|
|
params := url.Values{}
|
|
params.Add("format", fmt.Sprint(format))
|
|
baseUrl.RawQuery = params.Encode()
|
|
resp, err := http.Get(baseUrl.String())
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
return ""
|
|
}
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
return ""
|
|
}
|
|
return string(body)
|
|
}
|