@@ -11,7 +11,7 @@ This library is built using
11
11
data for the database record is decoded using this library. Version 2.0
12
12
provides significant performance improvements with 56% fewer allocations and
13
13
34% less memory usage compared to v1. Version 2.0 also adds ` Network ` and
14
- ` IPAddress ` fields to all result structs, and includes an ` IsZero ()` method to
14
+ ` IPAddress ` fields to all result structs, and includes a ` HasData ()` method to
15
15
easily check if data was found. If you only need several fields, you may get
16
16
superior performance by using maxminddb's ` Lookup ` directly with a result
17
17
struct that only contains the required fields. (See
@@ -32,7 +32,7 @@ Version 2.0 includes several major improvements:
32
32
- ** Modern API** : Uses ` netip.Addr ` instead of ` net.IP ` for better performance
33
33
- ** Network Information** : All result structs now include ` Network ` and
34
34
` IPAddress ` fields
35
- - ** Data Validation** : New ` IsZero ()` method to easily check if data was found
35
+ - ** Data Validation** : New ` HasData ()` method to easily check if data was found
36
36
- ** Structured Names** : Replaced ` map[string]string ` with typed ` Names ` struct
37
37
for better performance
38
38
- ** Go 1.24 Support** : Uses ` omitzero ` JSON tags to match MaxMind database
@@ -71,7 +71,7 @@ func main() {
71
71
if err != nil {
72
72
log.Fatal (err)
73
73
}
74
- if record.IsZero () {
74
+ if ! record.HasData () {
75
75
fmt.Println (" No data found for this IP" )
76
76
return
77
77
}
@@ -82,7 +82,9 @@ func main() {
82
82
fmt.Printf (" Russian country name: %v \n " , record.Country .Names .Russian )
83
83
fmt.Printf (" ISO country code: %v \n " , record.Country .ISOCode )
84
84
fmt.Printf (" Time zone: %v \n " , record.Location .TimeZone )
85
- fmt.Printf (" Coordinates: %v , %v \n " , record.Location .Latitude , record.Location .Longitude )
85
+ if record.Location .HasCoordinates () {
86
+ fmt.Printf (" Coordinates: %v , %v \n " , *record.Location .Latitude , *record.Location .Longitude )
87
+ }
86
88
// Output:
87
89
// Portuguese (BR) city name: Londres
88
90
// English subdivision name: England
@@ -151,7 +153,7 @@ func main() {
151
153
log.Fatal (err)
152
154
}
153
155
154
- if record.IsZero () {
156
+ if ! record.HasData () {
155
157
fmt.Println (" No data found for this IP" )
156
158
return
157
159
}
@@ -161,7 +163,9 @@ func main() {
161
163
fmt.Printf (" Country: %v (%v )\n " , record.Country .Names .English , record.Country .ISOCode )
162
164
fmt.Printf (" Continent: %v (%v )\n " , record.Continent .Names .English , record.Continent .Code )
163
165
fmt.Printf (" Postal Code: %v \n " , record.Postal .Code )
164
- fmt.Printf (" Location: %v , %v \n " , record.Location .Latitude , record.Location .Longitude )
166
+ if record.Location .HasCoordinates () {
167
+ fmt.Printf (" Location: %v , %v \n " , *record.Location .Latitude , *record.Location .Longitude )
168
+ }
165
169
fmt.Printf (" Time Zone: %v \n " , record.Location .TimeZone )
166
170
fmt.Printf (" Network: %v \n " , record.Traits .Network )
167
171
fmt.Printf (" IP Address: %v \n " , record.Traits .IPAddress )
@@ -200,7 +204,7 @@ func main() {
200
204
log.Fatal (err)
201
205
}
202
206
203
- if record.IsZero () {
207
+ if ! record.HasData () {
204
208
fmt.Println (" No data found for this IP" )
205
209
return
206
210
}
@@ -251,7 +255,7 @@ func main() {
251
255
log.Fatal (err)
252
256
}
253
257
254
- if record.IsZero () {
258
+ if ! record.HasData () {
255
259
fmt.Println (" No data found for this IP" )
256
260
return
257
261
}
@@ -296,7 +300,7 @@ func main() {
296
300
log.Fatal (err)
297
301
}
298
302
299
- if record.IsZero () {
303
+ if ! record.HasData () {
300
304
fmt.Println (" No data found for this IP" )
301
305
return
302
306
}
@@ -345,15 +349,17 @@ func main() {
345
349
log.Fatal (err)
346
350
}
347
351
348
- if record.IsZero () {
352
+ if ! record.HasData () {
349
353
fmt.Println (" No data found for this IP" )
350
354
return
351
355
}
352
356
353
357
// Basic location information
354
358
fmt.Printf (" City: %v \n " , record.City .Names .English )
355
359
fmt.Printf (" Country: %v (%v )\n " , record.Country .Names .English , record.Country .ISOCode )
356
- fmt.Printf (" Location: %v , %v \n " , record.Location .Latitude , record.Location .Longitude )
360
+ if record.Location .HasCoordinates () {
361
+ fmt.Printf (" Location: %v , %v \n " , *record.Location .Latitude , *record.Location .Longitude )
362
+ }
357
363
358
364
// Enterprise-specific fields
359
365
fmt.Printf (" ISP: %v \n " , record.Traits .ISP )
@@ -410,7 +416,7 @@ func main() {
410
416
log.Fatal (err)
411
417
}
412
418
413
- if record.IsZero () {
419
+ if ! record.HasData () {
414
420
fmt.Println (" No data found for this IP" )
415
421
return
416
422
}
@@ -464,7 +470,7 @@ func main() {
464
470
log.Fatal (err)
465
471
}
466
472
467
- if record.IsZero () {
473
+ if ! record.HasData () {
468
474
fmt.Println (" No data found for this IP" )
469
475
return
470
476
}
@@ -507,7 +513,7 @@ func main() {
507
513
log.Fatal (err)
508
514
}
509
515
510
- if record.IsZero () {
516
+ if ! record.HasData () {
511
517
fmt.Println (" No data found for this IP" )
512
518
return
513
519
}
@@ -551,7 +557,7 @@ func main() {
551
557
}
552
558
553
559
// Always check if data was found
554
- if record.IsZero () {
560
+ if ! record.HasData () {
555
561
fmt.Println (" No data found for this IP address" )
556
562
return
557
563
}
@@ -627,7 +633,7 @@ fmt.Println(string(jsonData))
627
633
- ** IP Type** : Use ` netip.Addr ` instead of ` net.IP `
628
634
- ** Field Names** : ` IsoCode ` → ` ISOCode `
629
635
- ** Names Access** : Use struct fields instead of map access
630
- - ** Data Validation** : Use ` IsZero ()` method to check for data availability
636
+ - ** Data Validation** : Use ` HasData ()` method to check for data availability
631
637
632
638
### Migration Example
633
639
@@ -647,7 +653,7 @@ if err != nil {
647
653
// handle error
648
654
}
649
655
record , err := db.City (ip)
650
- if record.IsZero () {
656
+ if ! record.HasData () {
651
657
// handle no data found
652
658
}
653
659
cityName := record.City .Names .English
@@ -659,7 +665,7 @@ cityName := record.City.Names.English
659
665
660
666
** Database not found** : Ensure the .mmdb file path is correct and readable.
661
667
662
- ** No data returned** : Check if ` IsZero ()` returns true - the IP may not be in
668
+ ** No data returned** : Check if ` HasData ()` returns false - the IP may not be in
663
669
the database or may be a private/reserved IP.
664
670
665
671
** Performance issues** : Ensure you're reusing the database instance rather than
0 commit comments