This gist demonstrates how to test the availability of a TCP endponit using Golang. The following example checks if an endpoint is available and responding to both TCP and TLS connections. To run the example, simply open a command line and execute the following:
$ curl -sSL https://goo.gl/ENx9d8 > tcp-tls-ping.go && go run tcp-tls-ping.go
tcp success
tls success
The program shows that the specified endpoint (defaults to google.com:443
) is available and responding to TCP and TLS connection attempts. To illustrate the error that occurs when an endpoint presents an invalid TLS certificate, execute the following:
$ curl -sSL https://goo.gl/ENx9d8 > tcp-tls-ping.go && go run tcp-tls-ping.go www.pcwebshop.co.uk:443
tcp success
tls error: x509: certificate is valid for *.secure-secure.co.uk, secure-secure.co.uk, not www.pcwebshop.co.uk
exit status 1
Finally, the program can also demonstrate that a TCP endpoint is unavailable:
$ curl -sSL https://goo.gl/ENx9d8 > tcp-tls-ping.go && go run tcp-tls-ping.go google.com:444
tcp error: dial tcp [2607:f8b0:4000:80a::200e]:444: i/o timeout
exit status 1