Created
June 17, 2022 13:09
-
-
Save bhavanki/9920e4bdfcaae3b88c7c2398da5d05bb to your computer and use it in GitHub Desktop.
A small Go program to find a free / unused TCP port
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 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