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