18 lines
291 B
Go
18 lines
291 B
Go
|
package net
|
||
|
|
||
|
import "net"
|
||
|
|
||
|
func GetFreeTcpPort() (int, error) {
|
||
|
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
l, err := net.ListenTCP("tcp", addr)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
defer l.Close()
|
||
|
return l.Addr().(*net.TCPAddr).Port, nil
|
||
|
}
|