-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipinfo-go.go
44 lines (39 loc) · 877 Bytes
/
ipinfo-go.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
package ipinfogo
import (
"bufio"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
)
type Ipinfo struct {
Ip string `json:"ip"`
City string `json:"city"`
Country string `json:"country"`
Loc string `json:"loc"`
Org string `json:"org"`
Postal string `json:"postal"`
Timezone string `json:"timezone"`
Readme string `json:"readme"`
}
func IpinfoRun() Ipinfo {
web, err := http.Get("https://ipinfo.io")
if err != nil {
log.Fatalln(err)
}
scanner := bufio.NewScanner(web.Body)
var getInfo string
for i := 0; scanner.Scan() && i < 11; i++ {
// fmt.Println(scanner.Text())
getInfo = fmt.Sprintln(getInfo + scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatalln(err)
}
web.Body.Close()
getInfo = strings.TrimSpace(getInfo)
out := Ipinfo{}
json.Unmarshal([]byte(getInfo), &out)
return out
}