-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (65 loc) · 1.98 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"time"
)
type VIACepResponse struct {
Cep string `json:"cep"`
Logradouro string `json:"logradouro"`
Complemento string `json:"complemento"`
Bairro string `json:"bairro"`
Localidade string `json:"localidade"`
Uf string `json:"uf"`
Ibge string `json:"ibge"`
Gia string `json:"gia"`
Ddd string `json:"ddd"`
Siafi string `json:"siafi"`
}
type BrasilAPIResponse struct {
Cep string `json:"cep"`
State string `json:"state"`
City string `json:"city"`
Neighborhood string `json:"neighborhood"`
Street string `json:"street"`
Service string `json:"service"`
}
func main() {
if len(os.Args) < 2 {
fmt.Println("CEP Obrigatorio para consulta")
panic("CEP Obrigatorio para consulta")
}
cep := os.Args[1]
viaCepResponse := VIACepResponse{}
brazilAPIResponse := BrasilAPIResponse{}
uriViacep := fmt.Sprintf("http://viacep.com.br/ws/%s/json/", cep)
uriBrazilapi := fmt.Sprintf("https://brasilapi.com.br/api/cep/v1/%s", cep)
channelViacep := make(chan interface{})
channelBrazilapi := make(chan interface{})
go execute(uriViacep, channelViacep, &viaCepResponse)
go execute(uriBrazilapi, channelBrazilapi, &brazilAPIResponse)
select {
case msg := <-channelViacep:
fmt.Printf("A API mais rapida foi a VIACep, e o retorno foi %s", msg)
case msg := <-channelBrazilapi:
fmt.Printf("A API mais rapida foi a BrazilAPI, e o retorno foi %s", msg)
case <-time.After(time.Second):
fmt.Printf("Nenhuma das apis respondeu no tempo configurado")
}
}
func execute(uri string, ch chan interface{}, responseInterface interface{}) {
req, err := http.Get(uri)
if err != nil {
fmt.Printf("Erro ao realizar consulta na url %s", uri)
panic(err)
}
defer req.Body.Close()
err = json.NewDecoder(req.Body).Decode(&responseInterface)
if err != nil {
fmt.Printf("Erro ao decodificar JSON da URL %s", uri)
panic(err)
}
ch <- responseInterface
}