Skip to content

Commit 30a393a

Browse files
committed
Add ExampleReader_NetworksWithin()
1 parent dfc02ff commit 30a393a

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

example_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,46 @@ func ExampleReader_Networks() {
155155
// 2003::/24: Cable/DSL
156156

157157
}
158+
159+
// This example demonstrates how to iterate over all networks in the
160+
// database which are contained within an arbitrary network.
161+
func ExampleReader_NetworksWithin() {
162+
db, err := maxminddb.Open("test-data/test-data/GeoIP2-Connection-Type-Test.mmdb")
163+
if err != nil {
164+
log.Fatal(err)
165+
}
166+
defer db.Close()
167+
168+
record := struct {
169+
Domain string `maxminddb:"connection_type"`
170+
}{}
171+
172+
_, network, err := net.ParseCIDR("1.0.0.0/8")
173+
if err != nil {
174+
log.Fatal(err)
175+
}
176+
177+
networks := db.NetworksWithin(network)
178+
for networks.Next() {
179+
subnet, err := networks.Network(&record)
180+
if err != nil {
181+
log.Fatal(err)
182+
}
183+
fmt.Printf("%s: %s\n", subnet.String(), record.Domain)
184+
}
185+
if networks.Err() != nil {
186+
log.Fatal(networks.Err())
187+
}
188+
189+
// Output:
190+
//1.0.0.0/24: Dialup
191+
//1.0.1.0/24: Cable/DSL
192+
//1.0.2.0/23: Dialup
193+
//1.0.4.0/22: Dialup
194+
//1.0.8.0/21: Dialup
195+
//1.0.16.0/20: Dialup
196+
//1.0.32.0/19: Dialup
197+
//1.0.64.0/18: Dialup
198+
//1.0.128.0/17: Dialup
199+
200+
}

0 commit comments

Comments
 (0)