GeoIP2 Reader for Go
This library reads MaxMind GeoLite2
and GeoIP2 databases.
This is not an official MaxMind API.
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"
"net"
)
func main() {
db, err := geoip2.Open("GeoIP2-City.mmdb")
if err != nil {
panic(err)
}
ip := net.ParseIP("81.2.69.142")
record, err := db.City(ip)
if err != nil {
panic(err)
}
fmt.Printf("Portuguese (BR) city name: %v\n", record.City.Names["pt-BR"])
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)
db.Close()
}
Notes
This uses the maxminddb library.
Contributing
Contributions welcome! Please fork the repository and open a pull request
with your changes.
License
This is free software, licensed under the Apache License, Version 2.0.