I'm Sending get requests to websites and get the statuscodes inside a loop, when the loop sends get request to a non-exist website, it breaks the program.
go version go1.12.6 darwin/amd64
func getRequest(url string) int {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
resp, err := http.Get(url)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
return resp.StatusCode
}
func checker(info []accountinfo) {
var request string
var statusCode int
for i := range info {
request = fmt.Sprintf("https://%s/", info[i].domain)
statusCode = getRequest(request)
if statusCode == 200 {
fmt.Println("That's ok")
} else {
fmt.Println(info[i].domain, "not found or content is not valid")
}
}
}
I expect it print "something.com not found or content is not valid". But it throws the error below.
2019/09/05 17:19:43 Get https://something.com/: dial tcp ip:443: connect: connection refused
exit status 1
EDIT
Using Println instead of Fatalln gives another error:
2019/09/05 17:52:18 Get https://something.com/: dial tcp ip:443: connect: connection refused
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x124eebf]
goroutine 1 [running]:
main.getRequest(0xc0000ae0e0, 0x1a, 0x0)
/Users/ozan/monitoring-development/v2/main.go:147 +0xff
main.checker(0xc000584000, 0x8d, 0x100)
/Users/ozan/monitoring-development/v2/main.go:110 +0xd3
main.main()
/Users/ozan/monitoring-development/v2/main.go:67 +0x4dd
exit status 2