25 lines
484 B
Go
25 lines
484 B
Go
package util
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
type LocationResult struct {
|
|
Country string `json:"country"`
|
|
Region string `json:"regionName"`
|
|
City string `json:"city"`
|
|
}
|
|
|
|
func GetLocation(ip string) (LocationResult, error) {
|
|
var loc LocationResult
|
|
// Example using ipinfo.io free API
|
|
resp, err := http.Get("https://ipinfo.io/" + ip + "/json")
|
|
if err != nil {
|
|
return loc, err
|
|
}
|
|
defer resp.Body.Close()
|
|
json.NewDecoder(resp.Body).Decode(&loc)
|
|
return loc, nil
|
|
}
|