GetAvailablePort finds and returns an available port on the system
()
| 1121 | |
| 1122 | // GetAvailablePort finds and returns an available port on the system |
| 1123 | func GetAvailablePort() (uint32, error) { |
| 1124 | // Use port 0 to let the OS assign an available port |
| 1125 | listener, err := net.Listen("tcp", ":0") |
| 1126 | if err != nil { |
| 1127 | return 0, fmt.Errorf("failed to find available port: %w", err) |
| 1128 | } |
| 1129 | defer listener.Close() |
| 1130 | |
| 1131 | // Extract the port from the listener's address |
| 1132 | addr := listener.Addr().(*net.TCPAddr) |
| 1133 | return uint32(addr.Port), nil |
| 1134 | } |
| 1135 | |
| 1136 | // isPortAvailable checks if a specific port is available on the system |
| 1137 | func isPortAvailable(port uint32) bool { |
no test coverage detected