country/country.go
func GetCountry(timeout uint64, url, field string) (string, error) {
	ctx, cancel := context.WithTimeout(
		context.Background(), time.Millisecond*time.Duration(timeout))
	defer cancel()

	req, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		return "", err
	}
	res, err := do(ctx, req)
	if err != nil {
		return "", err
	}
	defer res.Body.Close()

	object := make(map[string]interface{})
	if err := json.NewDecoder(res.Body).Decode(&object); err != nil {
		return "", err
	}

	f, ok := object[field]
	if !ok {
		return "", ErrMissingRequiredField
	}

	country, ok := f.(string)
	if !ok {
		return "", ErrBadCountryValueType
	}

	return strings.TrimSpace(strings.ToUpper(country)), nil
}
func GetCountry(timeout uint64, url, field string) (string, error) {
	ctx, cancel := context.WithTimeout(
		context.Background(), time.Millisecond*time.Duration(timeout))
	defer cancel()

	req, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		return "", err
	}
	res, err := do(ctx, req)
	if err != nil {
		return "", err
	}
	defer res.Body.Close()

	object := make(map[string]interface{})
	if err := json.NewDecoder(res.Body).Decode(&object); err != nil {
		return "", err
	}

	f, ok := object[field]
	if !ok {
		return "", ErrMissingRequiredField
	}

	country, ok := f.(string)
	if !ok {
		return "", ErrBadCountryValueType
	}

	return strings.TrimSpace(strings.ToUpper(country)), nil
}
func GetCountry ( timeout uint64 , url , field string ) ( string , error ) { ctx , cancel := context . WithTimeout ( context . Background ( ) , time . Millisecond * time . Duration ( timeout ) ) defer cancel ( ) req , err := http . NewRequest ( http . MethodGet , url , nil ) if err != nil { return " " , err } res , err := do ( ctx , req ) if err != nil { return " " , err } defer res . Body . Close ( ) object := make ( map [ string ] interface { } ) if err := json . NewDecoder ( res . Body ) . Decode ( & object ) ; err != nil { return " " , err } f , ok := object [ field ] if ! ok { return " " , ErrMissingRequiredField } country , ok := f . ( string ) if ! ok { return " " , ErrBadCountryValueType } return strings . TrimSpace ( strings . ToUpper ( country ) ) , nil }
// GetCountry returns country code by ip.
// Parses response in JSON format and returns a value of the field.
GetCountry returns country code by ip . Parses response in JSON format and returns a value of the field .