Monday, April 29, 2024
HomeGolangEasy methods to test if port is open or not? - Getting...

Easy methods to test if port is open or not? – Getting Assist


I’m completely new to Go and need to test if a port is open or not. I do know there are loads Portscanner implementations on the market however all of them differ in some particulars

  • internet.DialTimeout()
  • internet.Hear()

and a few of them assume that if an error was thrown the operate ought to return false. However is that this assumption right? I began with this implementation

func IsPortOpen(community string, tackle string, timeoutInMilliseconds int) bool {
	timeout := time.Length(timeoutInMilliseconds) * time.Millisecond
	
	connection, connectionError := internet.DialTimeout(community, tackle, timeout)

	if connectionError != nil {
		// return error or false?
	}

	defer func() {
		connectionCloseError := connection.Shut()

		if connectionCloseError != nil {
			// return error?
		}
	}()

	return true // is it right to return true or ought to we test for connection != nil ?
}

would somebody thoughts serving to implementing the operate so it’s working appropriately?

Thanks prematurely

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments