golang轻量级ISO-3166国家代码查询插件库go-countries的使用
golang轻量级ISO-3166国家代码查询插件库go-countries的使用
go-countries是一个轻量级的ISO-3166国家代码查询库。每个单元都实现了driver.Valuer
、ozzo validation.Validate
、Stringer
和json.Unmarshaler
接口。
这个库的创建目的是为了方便搜索和传输ISO-3166国家代码。
安装
go get github.com/mikekonan/go-countries
使用示例
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/mikekonan/go-countries"
"github.com/go-ozzo/ozzo-validation"
)
// 1. 在结构体中使用
type User struct {
Name string `json:"name" db:"name"`
Country country.Alpha2Code `json:"country" db:"country"`
}
func main() {
user := User{}
// 2. 在JSON中使用
err := json.Unmarshal([]byte(`{"name":"name", "country": "ca"}`), &user)
if err != nil {
log.Fatal(err)
}
// 3. 检查是否设置
fmt.Println("Country is set:", user.Country.IsSet()) // 检查用户国家是否提供
// 4. 使用ozzo-validation验证
if err := validation.ValidateStruct(&user,
validation.Field(&user.Country, validation.Required, user.Country)); err != nil {
log.Fatal(err)
}
// 5. 通过alpha2、alpha3或国家名称查询
if userCountry, ok := country.ByAlpha2Code(user.Country); ok {
fmt.Printf("国家名称: '%s', alpha-2代码: '%s', alpha-3代码: '%s'\n",
userCountry.Name(), userCountry.Alpha2Code(), userCountry.Alpha3Code())
}
// 6. 存储到数据库
fmt.Println("Database value:", user.Country.Value()) // 输出: 'CA'
// 7. 使用特定国家
fmt.Println("Canada alpha2 code:", country.Canada.Alpha2Code())
}
API
查询方法:
// 通过名称查询
country.ByNameStrErr()
country.ByNameErr()
country.ByNameStr()
country.ByName()
// 通过Alpha2代码查询
country.ByAlpha2CodeStrErr()
country.ByAlpha2CodeErr()
country.ByAlpha2CodeStr()
country.ByAlpha2Code()
// 通过Alpha3代码查询
country.ByAlpha3CodeStrErr()
country.ByAlpha3CodeErr()
country.ByAlpha3CodeStr()
country.ByAlpha3Code()
常量:
// 示例常量
country.Canada
country.Alpha2CA
country.Alpha3CAN
country.NameCanada
类型:
type Country struct {
name Name
alpha2 Alpha2Code
alpha3 Alpha3Code
}
func (c Country) Name() Name { return c.name }
func (c Country) Alpha2Code() Alpha2Code { return c.alpha2 }
func (c Country) Alpha3Code() Alpha3Code { return c.alpha3 }
func (c Country) NameStr() string { return c.name.String() }
func (c Country) Alpha2CodeStr() string { return c.alpha2.String() }
func (c Country) Alpha3CodeStr() string { return c.alpha3.String() }
更多关于golang轻量级ISO-3166国家代码查询插件库go-countries的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
更多关于golang轻量级ISO-3166国家代码查询插件库go-countries的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
go-countries: 轻量级ISO-3166国家代码查询库
go-countries 是一个轻量级的Golang库,用于查询ISO-3166标准定义的国家代码信息。它提供了简单易用的API来获取国家名称、2字母代码(alpha-2)、3字母代码(alpha-3)、数字代码、电话区号等信息。
主要特性
- 支持ISO-3166-1标准
- 内存占用低,无外部依赖
- 线程安全
- 支持按多种条件查询
- 包含250多个国家和地区信息
安装
go get github.com/biter777/countries
基本用法
1. 导入包
import "github.com/biter777/countries"
2. 通过国家代码查询
package main
import (
"fmt"
"github.com/biter777/countries"
)
func main() {
// 通过Alpha-2代码查询
country := countries.ByAlpha2Code("US")
fmt.Println("Country:", country) // United States
fmt.Println("Alpha-3:", country.Alpha3()) // USA
fmt.Println("Numeric:", country.Numeric()) // 840
fmt.Println("Currency:", country.Currency()) // USD
fmt.Println("CallingCode:", country.Call()) // 1
// 通过Alpha-3代码查询
country = countries.ByAlpha3Code("CAN")
fmt.Println("\nCountry:", country) // Canada
// 通过数字代码查询
country = countries.ByNumeric(826)
fmt.Println("\nCountry:", country) // United Kingdom
}
3. 获取所有国家列表
func main() {
allCountries := countries.All()
for _, country := range allCountries {
fmt.Printf("%s (%s) - %d\n",
country,
country.Alpha2(),
country.Numeric())
}
}
4. 高级查询示例
func main() {
// 查找使用欧元作为货币的国家
euroCountries := countries.FindByCurrency("EUR")
for _, c := range euroCountries {
fmt.Println(c, c.Alpha2())
}
// 查找电话区号为1的国家
callCode1 := countries.FindByCallingCode(1)
for _, c := range callCode1 {
fmt.Println(c, c.Call())
}
// 查找名称包含"United"的国家
unitedCountries := countries.FindByName("United")
for _, c := range unitedCountries {
fmt.Println(c)
}
}
5. 验证国家代码
func main() {
// 验证Alpha-2代码
valid := countries.ExistAlpha2("JP")
fmt.Println("JP is valid:", valid) // true
// 验证Alpha-3代码
valid = countries.ExistAlpha3("XYZ")
fmt.Println("XYZ is valid:", valid) // false
}
性能考虑
go-countries 在初始化时会将所有国家数据加载到内存中,但由于数据量不大(约250个条目),内存占用非常小。所有的查询操作都是O(1)时间复杂度,适合高性能场景。
自定义使用
如果需要扩展或修改数据,可以fork项目并修改内部的internal/data
包中的数据。
替代方案
如果go-countries不满足需求,也可以考虑以下替代库:
总结
go-countries 是一个简单易用的国家代码查询库,适合需要处理国际化、地理位置或电话号码解析的应用程序。它的轻量级设计和简单API使得集成非常方便。
更多详细用法请参考项目的GitHub页面。