func getAvailableTCPPort(port int) (int, error) {
	for i := 0; i <= 10; i++ {
		ln, err := net.Listen("tcp4", fmt.Sprintf("127.0.0.1:%d", port))
		if err != nil {
			return 0, err
		}
		defer ln.Close()
		addr := ln.Addr().String()
		addrParts := strings.SplitN(addr, ":", 2)
		p, err := strconv.Atoi(addrParts[1])
		if err != nil {
			return 0, err
		}
		if p != 0 {
			port = p
			return port, nil
		}
		port = 0 // Throw away the port hint before trying again
		time.Sleep(1)
	}
	return 0, fmt.Errorf("unable to allocate tcp port")
}
func getAvailableTCPPort(port int) (int, error) {
	for i := 0; i <= 10; i++ {
		ln, err := net.Listen("tcp4", fmt.Sprintf("127.0.0.1:%d", port))
		if err != nil {
			return 0, err
		}
		defer ln.Close()
		addr := ln.Addr().String()
		addrParts := strings.SplitN(addr, ":", 2)
		p, err := strconv.Atoi(addrParts[1])
		if err != nil {
			return 0, err
		}
		if p != 0 {
			port = p
			return port, nil
		}
		port = 0 
		time.Sleep(1)
	}
	return 0, fmt.Errorf("unable to allocate tcp port")
}
func getAvailableTCPPort ( port int ) ( int , error ) { for i := 0 ; i <= 10 ; i ++ { ln , err := net . Listen ( " " , fmt . Sprintf ( " " , port ) ) if err != nil { return 0 , err } defer ln . Close ( ) addr := ln . Addr ( ) . String ( ) addrParts := strings . SplitN ( addr , " " , 2 ) p , err := strconv . Atoi ( addrParts [ 1 ] ) if err != nil { return 0 , err } if p != 0 { port = p return port , nil } port = 0 // Throw away the port hint before trying again time . Sleep ( 1 ) } return 0 , fmt . Errorf ( " " ) }
// Select an available port, trying the specified
// port first, falling back on an OS selected port.
Select an available port trying the specified port first falling back on an OS selected port .