Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/oschwald/geoip2-golang
Unofficial MaxMind GeoIP2 Reader for Go
https://github.com/oschwald/geoip2-golang
database geoip geoip2 geolocation go maxmind maxmind-db
Last synced: 10 days ago
JSON representation
Unofficial MaxMind GeoIP2 Reader for Go
- Host: GitHub
- URL: https://github.com/oschwald/geoip2-golang
- Owner: oschwald
- License: isc
- Created: 2014-03-15T21:10:40.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2024-07-01T23:28:10.000Z (4 months ago)
- Last Synced: 2024-10-15T13:22:22.952Z (24 days ago)
- Topics: database, geoip, geoip2, geolocation, go, maxmind, maxmind-db
- Language: Go
- Homepage:
- Size: 147 KB
- Stars: 1,884
- Watchers: 32
- Forks: 193
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- my-awesome - oschwald/geoip2-golang - db pushed_at:2024-07 star:1.9k fork:0.2k Unofficial MaxMind GeoIP2 Reader for Go (Go)
- go-awesome - GeoIP2 Reader for Go - Parsing and reading of MaxMind GeoLite2 and GeoIP2 databases (Open source library / The Internet)
- awesome-golang-repositories - geoip2-golang
README
# GeoIP2 Reader for Go #
[![PkgGoDev](https://pkg.go.dev/badge/github.com/oschwald/geoip2-golang)](https://pkg.go.dev/github.com/oschwald/geoip2-golang)
This library reads MaxMind [GeoLite2](http://dev.maxmind.com/geoip/geoip2/geolite2/)
and [GeoIP2](http://www.maxmind.com/en/geolocation_landing) databases.This library is built using
[the Go maxminddb reader](https://github.com/oschwald/maxminddb-golang).
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](https://github.com/oschwald/maxminddb-golang/blob/main/example_test.go)
in the maxminddb repository for an example of this.)## Installation ##
```
go get github.com/oschwald/geoip2-golang
```## Usage ##
[See GoDoc](http://godoc.org/github.com/oschwald/geoip2-golang) for
documentation and examples.## Example ##
```go
package mainimport (
"fmt"
"log"
"net""github.com/oschwald/geoip2-golang"
)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.