forked from oschwald/maxminddb-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
141 lines (124 loc) · 3.06 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package maxminddb_test
import (
"fmt"
"log"
"net"
"github.com/oschwald/maxminddb-golang"
)
// This example shows how to decode to a struct
func ExampleReader_Lookup_struct() {
db, err := maxminddb.Open("test-data/test-data/GeoIP2-City-Test.mmdb")
if err != nil {
log.Fatal(err)
}
defer db.Close()
ip := net.ParseIP("81.2.69.142")
var record struct {
Country struct {
ISOCode string `maxminddb:"iso_code"`
} `maxminddb:"country"`
} // Or any appropriate struct
err = db.Lookup(ip, &record)
if err != nil {
log.Panic(err)
}
fmt.Print(record.Country.ISOCode)
// Output:
// GB
}
// This example demonstrates how to decode to an interface{}
func ExampleReader_Lookup_interface() {
db, err := maxminddb.Open("test-data/test-data/GeoIP2-City-Test.mmdb")
if err != nil {
log.Fatal(err)
}
defer db.Close()
ip := net.ParseIP("81.2.69.142")
var record interface{}
err = db.Lookup(ip, &record)
if err != nil {
log.Panic(err)
}
fmt.Printf("%v", record)
}
// This example demonstrates how to iterate over all networks in the
// database
func ExampleReader_Networks() {
db, err := maxminddb.Open("test-data/test-data/GeoIP2-Connection-Type-Test.mmdb")
if err != nil {
log.Fatal(err)
}
defer db.Close()
record := struct {
Domain string `maxminddb:"connection_type"`
}{}
networks := db.Networks(maxminddb.SkipAliasedNetworks)
for networks.Next() {
subnet, err := networks.Network(&record)
if err != nil {
log.Panic(err)
}
fmt.Printf("%s: %s\n", subnet.String(), record.Domain)
}
if networks.Err() != nil {
log.Panic(networks.Err())
}
// Output:
// 1.0.0.0/24: Dialup
// 1.0.1.0/24: Cable/DSL
// 1.0.2.0/23: Dialup
// 1.0.4.0/22: Dialup
// 1.0.8.0/21: Dialup
// 1.0.16.0/20: Dialup
// 1.0.32.0/19: Dialup
// 1.0.64.0/18: Dialup
// 1.0.128.0/17: Dialup
// 80.214.0.0/20: Cellular
// 96.1.0.0/16: Cable/DSL
// 96.10.0.0/15: Cable/DSL
// 96.69.0.0/16: Cable/DSL
// 96.94.0.0/15: Cable/DSL
// 108.96.0.0/11: Cellular
// 175.16.199.0/24: Dialup
// 187.156.138.0/24: Cable/DSL
// 201.243.200.0/24: Corporate
// 207.179.48.0/20: Cellular
// 2003::/24: Cable/DSL
}
// This example demonstrates how to iterate over all networks in the
// database which are contained within an arbitrary network.
func ExampleReader_NetworksWithin() {
db, err := maxminddb.Open("test-data/test-data/GeoIP2-Connection-Type-Test.mmdb")
if err != nil {
log.Fatal(err)
}
defer db.Close()
record := struct {
Domain string `maxminddb:"connection_type"`
}{}
_, network, err := net.ParseCIDR("1.0.0.0/8")
if err != nil {
log.Panic(err)
}
networks := db.NetworksWithin(network, maxminddb.SkipAliasedNetworks)
for networks.Next() {
subnet, err := networks.Network(&record)
if err != nil {
log.Panic(err)
}
fmt.Printf("%s: %s\n", subnet.String(), record.Domain)
}
if networks.Err() != nil {
log.Panic(networks.Err())
}
// Output:
// 1.0.0.0/24: Dialup
// 1.0.1.0/24: Cable/DSL
// 1.0.2.0/23: Dialup
// 1.0.4.0/22: Dialup
// 1.0.8.0/21: Dialup
// 1.0.16.0/20: Dialup
// 1.0.32.0/19: Dialup
// 1.0.64.0/18: Dialup
// 1.0.128.0/17: Dialup
}