Exemple #1
0
func main() {
	if len(os.Args) < 2 {
		log.Fatal("Please specify a city name!")
	}
	city := os.Args[1]
	temp, err := weather.Temperature(city)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(temp)
}
Exemple #2
0
func main() {
	http.HandleFunc("/weather/", func(w http.ResponseWriter, r *http.Request) {
		city := strings.SplitN(r.URL.Path, "/", 3)[2]

		temp, err := weather.Temperature(city)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		data := map[string]interface{}{
			"city": city,
			"temp": temp,
		}
		json.NewEncoder(w).Encode(data)
	})

	http.ListenAndServe(":8080", nil)
}