Cuaca API for Go.
Clean example request for the weather feature endpoint. Replace YOUR API and the city input, then run the code.
Checking
Clean example request for the weather feature endpoint. Replace YOUR API and the city input, then run the code.
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
)
const (
API_KEY = "YOUR API"
KOTA = "YOUR LOCATION"
ENDPOINT = "https://senpai-bot.store/cuaca"
)
type CuacaResult struct {
Celcius string `json:"celcius"`
Cuaca string `json:"cuaca"`
Kota string `json:"kota"`
Waktu string `json:"waktu"`
}
type CuacaResponse struct {
Code int `json:"code"`
Result CuacaResult `json:"result"`
Message string `json:"message,omitempty"`
}
func main() {
encodedKota := url.QueryEscape(KOTA)
fullURL := fmt.Sprintf("%s?apikey=%s&kota=%s", ENDPOINT, API_KEY, encodedKota)
resp, err := http.Get(fullURL)
if err != nil {
fmt.Println("Gagal request:", err)
os.Exit(1)
}
defer resp.Body.Close()
var result CuacaResponse
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
fmt.Println("Gagal parsing JSON:", err)
os.Exit(1)
}
if result.Code != 200 {
fmt.Printf("API error code: %d - %s\n", result.Code, result.Message)
os.Exit(1)
}
fmt.Println("Informasi Cuaca:")
fmt.Println("Kota :", result.Result.Kota)
fmt.Println("Suhu :", result.Result.Celcius+" °C")
fmt.Println("Cuaca :", result.Result.Cuaca)
fmt.Println("Waktu :", result.Result.Waktu)
}