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
| // GetFreePort asks the kernel for a free open port that is ready to use. | |
| func GetFreePort() (port int, err error) { | |
| var a *net.TCPAddr | |
| if a, err = net.ResolveTCPAddr("tcp", "localhost:0"); err == nil { | |
| var l *net.TCPListener | |
| if l, err = net.ListenTCP("tcp", a); err == nil { | |
| defer l.Close() | |
| return l.Addr().(*net.TCPAddr).Port, nil | |
| } | |
| } |