Skip to content

Commit ad304c1

Browse files
committed
Improve go docs
1 parent 4907b1d commit ad304c1

File tree

2 files changed

+384
-32
lines changed

2 files changed

+384
-32
lines changed

example_test.go

Lines changed: 242 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ func Example() {
1515
}
1616
defer db.Close()
1717
// If you are using strings that may be invalid, use netip.ParseAddr and check for errors
18-
ip := netip.MustParseAddr("81.2.69.142")
18+
ip, err := netip.ParseAddr("81.2.69.142")
19+
if err != nil {
20+
log.Panic(err)
21+
}
1922
record, err := db.City(ip)
2023
if err != nil {
2124
log.Panic(err)
@@ -34,3 +37,241 @@ func Example() {
3437
// Time zone: Europe/London
3538
// Coordinates: 51.5142, -0.0931
3639
}
40+
41+
// ExampleReader_City demonstrates how to use the City database.
42+
func ExampleReader_City() {
43+
db, err := Open("test-data/test-data/GeoIP2-City-Test.mmdb")
44+
if err != nil {
45+
log.Panic(err)
46+
}
47+
defer db.Close()
48+
49+
ip, err := netip.ParseAddr("81.2.69.142")
50+
if err != nil {
51+
log.Panic(err)
52+
}
53+
record, err := db.City(ip)
54+
if err != nil {
55+
log.Panic(err)
56+
}
57+
58+
if record.IsZero() {
59+
fmt.Println("No data found for this IP")
60+
return
61+
}
62+
63+
fmt.Printf("City: %v\n", record.City.Names.English)
64+
fmt.Printf("Country: %v (%v)\n", record.Country.Names.English, record.Country.ISOCode)
65+
fmt.Printf("Time zone: %v\n", record.Location.TimeZone)
66+
// Output:
67+
// City: London
68+
// Country: United Kingdom (GB)
69+
// Time zone: Europe/London
70+
}
71+
72+
// ExampleReader_Country demonstrates how to use the Country database.
73+
func ExampleReader_Country() {
74+
db, err := Open("test-data/test-data/GeoIP2-City-Test.mmdb")
75+
if err != nil {
76+
log.Panic(err)
77+
}
78+
defer db.Close()
79+
80+
ip, err := netip.ParseAddr("81.2.69.142")
81+
if err != nil {
82+
log.Panic(err)
83+
}
84+
record, err := db.Country(ip)
85+
if err != nil {
86+
log.Panic(err)
87+
}
88+
89+
if record.IsZero() {
90+
fmt.Println("No data found for this IP")
91+
return
92+
}
93+
94+
fmt.Printf("Country: %v (%v)\n", record.Country.Names.English, record.Country.ISOCode)
95+
fmt.Printf("Continent: %v (%v)\n", record.Continent.Names.English, record.Continent.Code)
96+
// Output:
97+
// Country: United Kingdom (GB)
98+
// Continent: Europe (EU)
99+
}
100+
101+
// ExampleReader_ASN demonstrates how to use the ASN database.
102+
func ExampleReader_ASN() {
103+
db, err := Open("test-data/test-data/GeoLite2-ASN-Test.mmdb")
104+
if err != nil {
105+
log.Panic(err)
106+
}
107+
defer db.Close()
108+
109+
ip, err := netip.ParseAddr("1.128.0.0")
110+
if err != nil {
111+
log.Panic(err)
112+
}
113+
record, err := db.ASN(ip)
114+
if err != nil {
115+
log.Panic(err)
116+
}
117+
118+
if record.IsZero() {
119+
fmt.Println("No data found for this IP")
120+
return
121+
}
122+
123+
fmt.Printf("ASN: %v\n", record.AutonomousSystemNumber)
124+
fmt.Printf("Organization: %v\n", record.AutonomousSystemOrganization)
125+
// Output:
126+
// ASN: 1221
127+
// Organization: Telstra Pty Ltd
128+
}
129+
130+
// ExampleReader_AnonymousIP demonstrates how to use the Anonymous IP database.
131+
func ExampleReader_AnonymousIP() {
132+
db, err := Open("test-data/test-data/GeoIP2-Anonymous-IP-Test.mmdb")
133+
if err != nil {
134+
log.Panic(err)
135+
}
136+
defer db.Close()
137+
138+
ip, err := netip.ParseAddr("1.2.0.0")
139+
if err != nil {
140+
log.Panic(err)
141+
}
142+
record, err := db.AnonymousIP(ip)
143+
if err != nil {
144+
log.Panic(err)
145+
}
146+
147+
if record.IsZero() {
148+
fmt.Println("No data found for this IP")
149+
return
150+
}
151+
152+
fmt.Printf("Is Anonymous: %v\n", record.IsAnonymous)
153+
fmt.Printf("Is Anonymous VPN: %v\n", record.IsAnonymousVPN)
154+
fmt.Printf("Is Public Proxy: %v\n", record.IsPublicProxy)
155+
// Output:
156+
// Is Anonymous: true
157+
// Is Anonymous VPN: true
158+
// Is Public Proxy: false
159+
}
160+
161+
// ExampleReader_Enterprise demonstrates how to use the Enterprise database.
162+
func ExampleReader_Enterprise() {
163+
db, err := Open("test-data/test-data/GeoIP2-Enterprise-Test.mmdb")
164+
if err != nil {
165+
log.Panic(err)
166+
}
167+
defer db.Close()
168+
169+
ip, err := netip.ParseAddr("74.209.24.0")
170+
if err != nil {
171+
log.Panic(err)
172+
}
173+
record, err := db.Enterprise(ip)
174+
if err != nil {
175+
log.Panic(err)
176+
}
177+
178+
if record.IsZero() {
179+
fmt.Println("No data found for this IP")
180+
return
181+
}
182+
183+
fmt.Printf("City: %v\n", record.City.Names.English)
184+
fmt.Printf("Country: %v (%v)\n", record.Country.Names.English, record.Country.ISOCode)
185+
fmt.Printf("ISP: %v\n", record.Traits.ISP)
186+
fmt.Printf("Organization: %v\n", record.Traits.Organization)
187+
// Output:
188+
// City: Chatham
189+
// Country: United States (US)
190+
// ISP: Fairpoint Communications
191+
// Organization: Fairpoint Communications
192+
}
193+
194+
// ExampleReader_ISP demonstrates how to use the ISP database.
195+
func ExampleReader_ISP() {
196+
db, err := Open("test-data/test-data/GeoIP2-ISP-Test.mmdb")
197+
if err != nil {
198+
log.Panic(err)
199+
}
200+
defer db.Close()
201+
202+
ip, err := netip.ParseAddr("1.128.0.0")
203+
if err != nil {
204+
log.Panic(err)
205+
}
206+
record, err := db.ISP(ip)
207+
if err != nil {
208+
log.Panic(err)
209+
}
210+
211+
if record.IsZero() {
212+
fmt.Println("No data found for this IP")
213+
return
214+
}
215+
216+
fmt.Printf("ISP: %v\n", record.ISP)
217+
fmt.Printf("Organization: %v\n", record.Organization)
218+
fmt.Printf("ASN: %v\n", record.AutonomousSystemNumber)
219+
// Output:
220+
// ISP: Telstra Internet
221+
// Organization: Telstra Internet
222+
// ASN: 1221
223+
}
224+
225+
// ExampleReader_Domain demonstrates how to use the Domain database.
226+
func ExampleReader_Domain() {
227+
db, err := Open("test-data/test-data/GeoIP2-Domain-Test.mmdb")
228+
if err != nil {
229+
log.Panic(err)
230+
}
231+
defer db.Close()
232+
233+
ip, err := netip.ParseAddr("1.2.0.0")
234+
if err != nil {
235+
log.Panic(err)
236+
}
237+
record, err := db.Domain(ip)
238+
if err != nil {
239+
log.Panic(err)
240+
}
241+
242+
if record.IsZero() {
243+
fmt.Println("No data found for this IP")
244+
return
245+
}
246+
247+
fmt.Printf("Domain: %v\n", record.Domain)
248+
// Output:
249+
// Domain: maxmind.com
250+
}
251+
252+
// ExampleReader_ConnectionType demonstrates how to use the Connection Type database.
253+
func ExampleReader_ConnectionType() {
254+
db, err := Open("test-data/test-data/GeoIP2-Connection-Type-Test.mmdb")
255+
if err != nil {
256+
log.Panic(err)
257+
}
258+
defer db.Close()
259+
260+
ip, err := netip.ParseAddr("1.0.128.0")
261+
if err != nil {
262+
log.Panic(err)
263+
}
264+
record, err := db.ConnectionType(ip)
265+
if err != nil {
266+
log.Panic(err)
267+
}
268+
269+
if record.IsZero() {
270+
fmt.Println("No data found for this IP")
271+
return
272+
}
273+
274+
fmt.Printf("Connection Type: %v\n", record.ConnectionType)
275+
// Output:
276+
// Connection Type: Cable/DSL
277+
}

0 commit comments

Comments
 (0)