Go Report Card Build Status codecov
This library has been created with the purpose to facilitate the store, validation, and transfer of Go ISO-3166/ISO-4217/timezones/emails/URL types. There is a openapi3 spec of that type and make you able to include it into your spec. All types has own ozzo.Validate, json.Unmarshaler, Stringer and driver.Valuer implementations.
go get github.com/mikekonan/go-types/v2
package main import ( "encoding/json" "fmt" "log" validation "github.com/go-ozzo/ozzo-validation/v4" "github.com/mikekonan/go-types/v2/country" "github.com/mikekonan/go-types/v2/country/alpha2" "github.com/mikekonan/go-types/v2/country/alpha3" "github.com/mikekonan/go-types/v2/language" "github.com/mikekonan/go-types/v2/country/name" "github.com/mikekonan/go-types/v2/currency" "github.com/mikekonan/go-types/v2/currency/code" "github.com/mikekonan/go-types/v2/phone" "github.com/mikekonan/go-types/v2/postal_code" ) // 1. use in your structs type User struct { Name string `json:"name" db:"name"` Country country.Alpha2Code `json:"country" db:"country"` Currency currency.Code `json:"currency" db:"currency"` Language language.Alpha2Code `json:"language" db:"language"` Phone phone.Number `json:"phone" db:"phone"` CountryDialCode phone.DialCode `json:"dialCode" db:"dialCode"` PostalCode postalcode.PostalCode `json:"postalCode" db:"postalCode"` } func main() { // 2. use in your wire user := User{} _ = json.Unmarshal([]byte(`{"name":"name", "country": "CA", "currency": "CAD", "language": "fr", "phone": "123456789", "dialCode": "1"}`), &user) // 3. check is set user.Country.IsSet() user.Currency.IsSet() user.Language.IsSet() // 4. validate using ozzo-validation if err := validation.ValidateStruct(&user, validation.Field(&user.Country), validation.Field(&user.Currency)); err != nil { log.Fatal(err) } // 5. lookup by alpha2, alpha3, country name if userCountry, ok := country.ByAlpha2Code(user.Country); ok { fmt.Printf("country name - '%s', alpha-2 - '%s', alpha-3 - '%s'", userCountry.Name(), userCountry.Alpha2Code(), userCountry.Alpha3Code()) } // 6. lookup by 2 and 3 char codes, language name if userLanguage, ok := language.ByAlpha2Code(user.Language); ok { fmt.Printf("language name - '%s', alpha-2 - '%s', alpha-3 - '%s'", userLanguage.Name(), userLanguage.Alpha2Code(), userLanguage.Alpha3Code()) } // 7. lookup by country dial code if phoneCountries, ok := phone.CountriesByDialCode(user.CountryDialCode); ok { for _, phoneCountry := range phoneCountries { fmt.Printf("country by dial code - '%s'", phoneCountry) } } // 8. lookup by country if dialCode, ok := phone.DialByAlpha2Code(user.Country); ok { fmt.Printf("'%s' dial code is '%s'", user.Country, dialCode) } // 9. lookup by currency code if userCurrency, ok := currency.ByCode(user.Currency); ok { fmt.Printf("currency name - '%s', code - '%s', number - '%s', countries - '%s', decimal places - '%d'", userCurrency.Currency(), userCurrency.Code(), userCurrency.Number(), userCurrency.Countries(), userCurrency.DecimalPlaces()) } // 10. store in db fmt.Println(user.Country.Value()) //prints 'CA' fmt.Println(user.Currency.Value()) //prints 'CAN' fmt.Println(user.Language.Value()) //prints 'fr' // 11. use specific country constants fmt.Println(country.Canada.Alpha2Code()) fmt.Println("name:", name.Canada) fmt.Println("alpha-2:", alpha2.CA) fmt.Println("alpha-3:", alpha3.CAN) // 12. use specific currency codes fmt.Println(code.CAD) }
- Currency Codes (ISO 4217) - wiki, data source
- Country Codes (ISO 3166) - wiki
- URL(including HttpURL) (standard) - wiki
- Email (part of RFC5322) - wiki
- Timezone (RFC6557 IANA timezones) - wiki
- Languages (ISO 639-1) - wiki; (ISO 639-2) - wiki, data source
- Dial Codes (E.164) - wiki, data source
- BCP47 language tags (wiki), rfc