|
| 1 | +# Migrating to geoip2-golang v2 |
| 2 | + |
| 3 | +Version 2.0 modernizes the public API, improves performance, and tightens |
| 4 | +parity with current MaxMind databases. This guide highlights the most important |
| 5 | +changes and shows how to update existing v1 code. |
| 6 | + |
| 7 | +## Upgrade Checklist |
| 8 | + |
| 9 | +1. **Update imports** |
| 10 | + |
| 11 | + ```go |
| 12 | + // v1 |
| 13 | + import "github.com/oschwald/geoip2-golang" |
| 14 | + |
| 15 | + // v2 |
| 16 | + import "github.com/oschwald/geoip2-golang/v2" |
| 17 | + ``` |
| 18 | + |
| 19 | +2. **Switch to `netip.Addr`** |
| 20 | + |
| 21 | + Lookup methods now take `netip.Addr` instead of `net.IP`. Use |
| 22 | + `netip.ParseAddr` (errors on invalid input) or `netip.MustParseAddr` for |
| 23 | + trusted literals. This avoids ambiguity around IPv4-in-IPv6 addresses and |
| 24 | + removes heap allocations for most lookups. |
| 25 | + |
| 26 | + ```go |
| 27 | + // v1 |
| 28 | + ip := net.ParseIP("81.2.69.142") |
| 29 | + record, err := db.City(ip) |
| 30 | + |
| 31 | + // v2 |
| 32 | + ip, err := netip.ParseAddr("81.2.69.142") |
| 33 | + if err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + record, err := db.City(ip) |
| 37 | + ``` |
| 38 | + |
| 39 | +3. **Adopt the new `HasData()` helpers** |
| 40 | + |
| 41 | + All result structs have a `HasData()` method that ignores the always-filled |
| 42 | + `Network` and `IPAddress` fields. Use it instead of comparing against zero |
| 43 | + values. |
| 44 | + |
| 45 | + ```go |
| 46 | + record, err := db.City(ip) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + if !record.HasData() { |
| 51 | + // handle missing GeoIP data |
| 52 | + } |
| 53 | + ``` |
| 54 | + |
| 55 | +4. **Use structured `Names` fields** |
| 56 | + |
| 57 | + Localized names are now strongly typed instead of map lookups. Access the |
| 58 | + language-specific struct field directly. |
| 59 | + |
| 60 | + ```go |
| 61 | + // v1 |
| 62 | + cityEn := record.City.Names["en"] |
| 63 | + cityPtBr := record.City.Names["pt-BR"] |
| 64 | + |
| 65 | + // v2 |
| 66 | + cityEn := record.City.Names.English |
| 67 | + cityPtBr := record.City.Names.BrazilianPortuguese |
| 68 | + ``` |
| 69 | + |
| 70 | + Supported fields include `English`, `German`, `Spanish`, `French`, |
| 71 | + `Japanese`, `BrazilianPortuguese`, `Russian`, and `SimplifiedChinese`. |
| 72 | + |
| 73 | +5. **Expect `Network` and `IPAddress` on every result** |
| 74 | + |
| 75 | + Each lookup populates the queried IP address and the enclosing network on |
| 76 | + every result struct, even if `HasData()` returns false. Existing code that |
| 77 | + depended on empty `Network` for “no data” should switch to `HasData()`. |
| 78 | + |
| 79 | +6. **Adjust renamed fields** |
| 80 | + |
| 81 | + `IsoCode` is now `ISOCode` across all structs. Update code accordingly. |
| 82 | + |
| 83 | + ```go |
| 84 | + // v1 |
| 85 | + code := record.Country.IsoCode |
| 86 | + |
| 87 | + // v2 |
| 88 | + code := record.Country.ISOCode |
| 89 | + ``` |
| 90 | + |
| 91 | +7. **Build with Go 1.24 or newer** |
| 92 | + |
| 93 | + The module now depends on Go 1.24 features such as `omitzero` struct tags. |
| 94 | + Update your toolchain (`go env GOVERSION`) before upgrading. |
| 95 | + |
| 96 | +## Additional Notes |
| 97 | + |
| 98 | +- `Open` and `OpenBytes` accept optional `Option` values, allowing future |
| 99 | + extensions without new breaking releases. |
| 100 | +- The legacy `FromBytes` helper is removed. Use `OpenBytes` instead. |
| 101 | +- JSON output mirrors MaxMind database naming thanks to the new `omitzero` tags |
| 102 | + and typed structs. |
| 103 | + |
| 104 | +Following the checklist above should cover most migrations. For edge cases, |
| 105 | +review `models.go` for field definitions and `example_test.go` for idiomatic |
| 106 | +usage with the v2 API. |
0 commit comments