package freeportimport ()// GetFreePort asks the kernel for a free open port that is ready to use.func () (int, error) { , := net.ResolveTCPAddr("tcp", "localhost:0")if != nil {return0, } , := net.ListenTCP("tcp", )if != nil {return0, }defer .Close()return .Addr().(*net.TCPAddr).Port, nil}// GetPort is deprecated, use GetFreePort instead// Ask the kernel for a free open port that is ready to usefunc () int { , := GetFreePort()if != nil {panic() }return}// GetFreePort asks the kernel for free open ports that are ready to use.func ( int) ([]int, error) {var []intfor := 0; < ; ++ { , := net.ResolveTCPAddr("tcp", "localhost:0")if != nil {returnnil, } , := net.ListenTCP("tcp", )if != nil {returnnil, }defer .Close() = append(, .Addr().(*net.TCPAddr).Port) }return , nil}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.