forked from apilayer/freegeoip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
61 lines (56 loc) · 1.36 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
// Copyright 2009 The freegeoip authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package freegeoip
import (
"log"
"net"
"time"
)
func ExampleOpen() {
db, err := Open("./testdata.gz")
if err != nil {
log.Fatal(err)
}
var result customQuery
err = db.Lookup(net.ParseIP("8.8.8.8"), &result)
if err != nil {
log.Fatal(err)
}
defer db.Close()
log.Printf("%#v", result)
}
func ExampleOpenURL() {
updateInterval := 24 * time.Hour
maxRetryInterval := time.Hour
db, err := OpenURL(MaxMindDB, updateInterval, maxRetryInterval)
if err != nil {
log.Fatal(err)
}
defer db.Close()
select {
case <-db.NotifyOpen():
// Wait for the db to be downloaded.
case err := <-db.NotifyError():
log.Fatal(err)
}
var result customQuery
err = db.Lookup(net.ParseIP("8.8.8.8"), &result)
if err != nil {
log.Fatal(err)
}
log.Printf("%#v", result)
}
// A customQuery is the query executed in the maxmind database for
// every IP lookup request.
type customQuery struct {
Country struct {
ISOCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
} `maxminddb:"country"`
Location struct {
Latitude float64 `maxminddb:"latitude"`
Longitude float64 `maxminddb:"longitude"`
TimeZone string `maxminddb:"time_zone"`
} `maxminddb:"location"`
}