项目作者: oschwald

项目描述 :
Unofficial MaxMind GeoIP2 Reader for Go
高级语言: Go
项目地址: git://github.com/oschwald/geoip2-golang.git
创建时间: 2014-03-15T21:10:40Z
项目社区:https://github.com/oschwald/geoip2-golang

开源协议:ISC License

下载


GeoIP2 Reader for Go

PkgGoDev

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

  1. go get github.com/oschwald/geoip2-golang

Usage

See GoDoc for
documentation and examples.

Example

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net"
  6. "github.com/oschwald/geoip2-golang"
  7. )
  8. func main() {
  9. db, err := geoip2.Open("GeoIP2-City.mmdb")
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. defer db.Close()
  14. // If you are using strings that may be invalid, check that ip is not nil
  15. ip := net.ParseIP("81.2.69.142")
  16. record, err := db.City(ip)
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. fmt.Printf("Portuguese (BR) city name: %v\n", record.City.Names["pt-BR"])
  21. if len(record.Subdivisions) > 0 {
  22. fmt.Printf("English subdivision name: %v\n", record.Subdivisions[0].Names["en"])
  23. }
  24. fmt.Printf("Russian country name: %v\n", record.Country.Names["ru"])
  25. fmt.Printf("ISO country code: %v\n", record.Country.IsoCode)
  26. fmt.Printf("Time zone: %v\n", record.Location.TimeZone)
  27. fmt.Printf("Coordinates: %v, %v\n", record.Location.Latitude, record.Location.Longitude)
  28. // Output:
  29. // Portuguese (BR) city name: Londres
  30. // English subdivision name: England
  31. // Russian country name: Великобритания
  32. // ISO country code: GB
  33. // Time zone: Europe/London
  34. // Coordinates: 51.5142, -0.0931
  35. }

Testing

Make sure you checked out test data submodule:

  1. git submodule init
  2. git submodule update

Execute test suite:

  1. 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.