BMKG API for Go.
Clean example request for the BMKG feature endpoint. Replace YOUR API, then run the code.
Checking
Clean example request for the BMKG feature endpoint. Replace YOUR API, then run the code.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
const (
API_KEY = "YOUR API"
ENDPOINT = "https://senpai-bot.store/bmkg"
)
type Response struct {
Code int `json:"code"`
Result Result `json:"result"`
}
type Result struct {
Gempa []Gempa `json:"gempa"`
}
type Gempa struct {
Wilayah string `json:"wilayah"`
Waktu string `json:"waktu"`
Magnitude string `json:"magnitude"`
Kedalaman string `json:"kedalaman"`
Koordinat string `json:"koordinat"`
Potensi string `json:"potensi"`
}
func main() {
url := fmt.Sprintf("%s?apikey=%s", ENDPOINT, API_KEY)
resp, err := http.Get(url)
if err != nil {
fmt.Printf("Gagal request: %v\n", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Gagal membaca response body")
return
}
var res Response
if err := json.Unmarshal(body, &res); err != nil {
fmt.Println("Gagal parsing JSON")
return
}
if res.Code != 200 {
fmt.Printf("API error code: %d\n", res.Code)
return
}
if len(res.Result.Gempa) == 0 {
fmt.Println("Tidak ada data gempa ditemukan.")
return
}
fmt.Println("Informasi Gempa BMKG Terbaru:\n")
for i, gempa := range res.Result.Gempa {
fmt.Printf("Gempa #%d\n", i+1)
fmt.Println("Wilayah :", gempa.Wilayah)
fmt.Println("Waktu :", gempa.Waktu)
fmt.Println("Magnitudo :", gempa.Magnitude)
fmt.Println("Kedalaman :", gempa.Kedalaman)
fmt.Println("Koordinat :", gempa.Koordinat)
fmt.Println("Potensi :", gempa.Potensi)
fmt.Println()
}
}