geoip2-golang

Unofficial MaxMind GeoIP2 Reader for Go

Github星跟踪图

GeoIP2 Reader for Go

GoDoc

This library reads MaxMind GeoLite2
and GeoIP2 databases.

This library is built using
the Go maxminddb reader.
All data for the database record is decoded using this library. If you only
need several fields, you may get superior performance by using maxminddb's
Lookup directly with a result struct that only contains the required fields.
(See example_test.go
in the maxminddb repository for an example of this.)

Installation

go get github.com/oschwald/geoip2-golang

Usage

See GoDoc for
documentation and examples.

Example

package main

import (
	"fmt"
	"github.com/oschwald/geoip2-golang"
	"log"
	"net"
)

func main() {
	db, err := geoip2.Open("GeoIP2-City.mmdb")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()
	// If you are using strings that may be invalid, check that ip is not nil
	ip := net.ParseIP("81.2.69.142")
	record, err := db.City(ip)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Portuguese (BR) city name: %v\n", record.City.Names["pt-BR"])
	if len(record.Subdivisions) > 0 {
		fmt.Printf("English subdivision name: %v\n", record.Subdivisions[0].Names["en"])
	}
	fmt.Printf("Russian country name: %v\n", record.Country.Names["ru"])
	fmt.Printf("ISO country code: %v\n", record.Country.IsoCode)
	fmt.Printf("Time zone: %v\n", record.Location.TimeZone)
	fmt.Printf("Coordinates: %v, %v\n", record.Location.Latitude, record.Location.Longitude)
	// Output:
	// Portuguese (BR) city name: Londres
	// English subdivision name: England
	// Russian country name: Великобритания
	// ISO country code: GB
	// Time zone: Europe/London
	// Coordinates: 51.5142, -0.0931
}

Testing

Make sure you checked out test data submodule:

git submodule init
git submodule update

Execute test suite:

go test

Contributing

Contributions welcome! Please fork the repository and open a pull request
with your changes.

License

This is free software, licensed under the ISC license.

主要指标

概览
名称与所有者oschwald/geoip2-golang
主编程语言Go
编程语言Go (语言数: 1)
平台
许可证ISC License
所有者活动
创建于2014-03-15 21:10:40
推送于2025-03-24 19:00:01
最后一次提交
发布数14
最新版本名称v1.11.0 (发布于 )
第一版名称v0.1.0 (发布于 2014-05-05 18:46:12)
用户参与
星数2k
关注者数31
派生数204
提交数199
已启用问题?
问题数61
打开的问题数4
拉请求数45
打开的拉请求数3
关闭的拉请求数20
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?