Fix string format
This commit is contained in:
31
go-demo-5/weather/weather.go
Normal file
31
go-demo-5/weather/weather.go
Normal file
@@ -0,0 +1,31 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user