Quellcode durchsuchen

added new file go/l17.go To determine the weather using the OpenWeatherMap API

root vor 3 Wochen
Ursprung
Commit
7928267d37
3 geänderte Dateien mit 48 neuen und 0 gelöschten Zeilen
  1. 47 0
      go/l17.go
  2. BIN
      go/output/l17
  3. 1 0
      go/tempCodeRunnerFile.go

+ 47 - 0
go/l17.go

@@ -0,0 +1,47 @@
+package main
+
+import (
+	"log"
+	"github.com/joho/godotenv"
+	"os"
+	"strings"
+	"net/http"
+	"time"
+	"encoding/json"
+	"fmt"
+)
+
+type OpenWeather struct{
+	City string `json:"name"`
+	Main struct {
+		Temp float64 `json:"temp"`
+	} `json:"main"`
+}
+
+func main() {
+	err := godotenv.Load()
+	if err != nil {
+		log.Fatalf("%v\n", err)
+	}
+	api := os.Getenv("openweathermap")
+	city := os.Getenv("city_openweather")
+	client := &http.Client{
+		Timeout: 20 * time.Second,
+	}
+	link := "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=metric&appid=" + api
+	url := strings.ReplaceAll(link, " ", "%20")
+	req, err := http.NewRequest("GET", url, nil)
+	if err != nil {
+		log.Fatalf("%v\n", err)
+	}
+	resp, err := client.Do(req)
+	if err != nil {
+		log.Fatalf("%v\n", err)
+	}
+	var WeatherData OpenWeather
+	err = json.NewDecoder(resp.Body).Decode(&WeatherData)
+	if err != nil {
+		log.Fatalf("%v\n", err)
+	}
+	fmt.Println(WeatherData.City, WeatherData.Main.Temp)
+}

BIN
go/output/l17


+ 1 - 0
go/tempCodeRunnerFile.go

@@ -0,0 +1 @@
+link := "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=metric&appid=" + api