Skip to content

Instantly share code, notes, and snippets.

@bhavanki
Created June 17, 2022 13:09
Show Gist options
  • Save bhavanki/9920e4bdfcaae3b88c7c2398da5d05bb to your computer and use it in GitHub Desktop.
Save bhavanki/9920e4bdfcaae3b88c7c2398da5d05bb to your computer and use it in GitHub Desktop.
A small Go program to find a free / unused TCP port
package main
import (
"fmt"
"net"
"os"
)
func main() {
ln, err := net.Listen("tcp", "")
if err != nil {
fmt.Printf("%v", err)
os.Exit(1)
} else {
fmt.Printf("%d", ln.Addr().(*net.TCPAddr).Port)
ln.Close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment