25 lines
495 B
Go
25 lines
495 B
Go
package geo
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
type GeoInfo struct {
|
|
Country string `json:"country"`
|
|
Region string `json:"regionName"`
|
|
City string `json:"city"`
|
|
}
|
|
|
|
func GetGeoLocation() GeoInfo {
|
|
resp, err := http.Get("http://ip-api.com/json")
|
|
if err != nil {
|
|
return GeoInfo{Country: "Unknown", Region: "Unknown", City: "Unknown"}
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var info GeoInfo
|
|
json.NewDecoder(resp.Body).Decode(&info)
|
|
return info
|
|
}
|