Small network-oriented Go library.
go get github.com/dector/nettwUse ParsePortOrPickAnother():
package main
import (
"fmt"
"github.com/dector/nettw"
)
func main() {
port, err := nettw.ParsePortOrPickAnother("8080")
if err != nil {
panic(err)
}
// If port 8080 free - we will get it here.
// If not - random free port will be returned.
fmt.Printf("Using port: %d\n", port.Int)
}Use ParsePortOrPickAnotherWithArgs():
package main
import (
"fmt"
"github.com/dector/nettw"
)
func main() {
port, err := nettw.ParsePortOrPickAnotherWithArgs(
"8080",
nettw.ParsePortArgs{
NewPortFrom: 3000,
NewPortTo: 4000,
MaxTries: 50,
}
)
if err != nil {
panic(err)
}
fmt.Printf("Using port: %d\n", port.Int)
}This project is licensed under the MIT License.