portpicker

package
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2021 License: Apache-2.0 Imports: 2 Imported by: 1

Documentation

Overview

Package portpicker provides an available TCP or UDP port to use in a service above port 1023. It does not guarentee that the port will remain available.

Get a TCP port to bind on all valid IPs for binding:

port, err := TCP()
if err != nil {
	panic(err)
}

Same, but panic if it can't be done:

port := Must(TCP)

Get a UDP port on IP 192.168.0.1 and the IPV6 localhost:

port := Must(UDP, IP(net.ParseIP("192.168.0.1"), Local6()))

Get a TCP port on an IPv4 localhost:

port := Must(TCP, Local4())

When binding to localhost, use "localhost:<port>", don't assume [::1]:<port> or 127.0.0.1:<port. If you want that, use option IP("127.0.0.1") instead.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Must

func Must(f func(options ...Option) (int, error), options ...Option) int

Must is used to call either a passed TCP() or UDP() call and its options. If there is an error, it panics

func TCP

func TCP(options ...Option) (int, error)

TCP returns a TCP port that can be bound to. With no options passed it will bind to all IPs on the system. If an option is passed, it will only bind to the options specified.

func UDP

func UDP(options ...Option) (int, error)

UDP returns a UDP port that can be bound to. With no options passed it will bind to all IPs on the system. If an option is passed, it will only bind to the options specified.

Types

type Option

type Option func(o *opts)

Option is an optional argument.

func IP

func IP(ip net.IP) Option

IP specifies you want to specify a specific host address to bind to.

func Local4

func Local4() Option

Local4 informs we want to bind to the IPv4 localhost address.

func Local6

func Local6() Option

Local6 informs we want to bind to the IPv6 localhost address.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL