Created
January 7, 2016 08:21
-
-
Save montanaflynn/b59c058ce2adc18f31d6 to your computer and use it in GitHub Desktop.
Port package which finds available ports or checks if a port is available
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package port | |
import ( | |
"net" | |
"strconv" | |
) | |
// New gets an available port | |
func New() (port int, err error) { | |
// Create a new server without specifying a port | |
// which will result in an open port being chosen | |
server, err := net.Listen("tcp", ":0") | |
// If there's an error it likely means no ports | |
// are available or something else prevented finding | |
// an open port | |
if err != nil { | |
return 0, err | |
} | |
// Defer the closing of the server so it closes | |
defer server.Close() | |
// Get the host string in the format "127.0.0.1:4444" | |
hostString := server.Addr().String() | |
// Split the host from the port | |
_, portString, err := net.SplitHostPort(hostString) | |
if err != nil { | |
return 0, err | |
} | |
// Return the port as an int | |
return strconv.Atoi(portString) | |
} | |
// Check if a port is available | |
func Check(port int) (status bool, err error) { | |
// Concatenate a colon and the port | |
host := ":" + strconv.Itoa(port) | |
// Try to create a server with the port | |
server, err := net.Listen("tcp", host) | |
// if it fails then the port is likely taken | |
if err != nil { | |
return false, err | |
} | |
// close the server | |
server.Close() | |
// we successfully used and closed the port | |
// so it's now available to be used again | |
return true, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment