Created
August 8, 2018 12:19
-
-
Save Ellrion/e60e01c71ee5afdd2d99678353f192d2 to your computer and use it in GitHub Desktop.
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 freeport | |
import ( | |
"net" | |
"strconv" | |
) | |
func GetFreePort() (string, error) { | |
l, err := net.Listen("tcp", "127.0.0.1:0") | |
defer l.Close() | |
if err != nil { | |
return "", err | |
} | |
p := l.Addr().(*net.TCPAddr).Port | |
return strconv.Itoa(p), err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment