I had to create a periodic status check for an open port on a specific host for a service I was creating recently. The status check has to work for both the localhost in development and also on the remote host.
Using the [net][net pkg] package in Go I was able to come up with the following snippet for testing the localhost port:
We use [Listen][net pkg Listen] above as it works for localhost only.
For testing the remote port, we can use [DialTimeout][net pkg DialTimeout] as it accepts a custom timeout parameter, which we can use to check for timeout errors: