Skip to content

Commit 45a6c64

Browse files
committed
Improve go docs
1 parent 526d0d4 commit 45a6c64

File tree

2 files changed

+356
-31
lines changed

2 files changed

+356
-31
lines changed

example_test.go

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

0 commit comments

Comments
 (0)