Version: v0.0.0-...-068fdf5 Latest Latest

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

Go to latest
Published: May 30, 2020 License: Apache-2.0 Imports: 4 Imported by: 0



Package hostandport contains utility functions for working with net addresses (numeric IP and domain names).

package main

import (


func main() {
	hp, _ := hostandport.FromString("[2001:db8::1]")
	hp, _ = hp.WithDefaultPort(80)
	hp, _ = hp.RequireBracketsForIPv6()

	port, _ := hp.Port()





View Source
const NoPort = -1

NoPort indicates the absence of a port number.


This section is empty.


This section is empty.


type HostAndPort

type HostAndPort struct {
	// contains filtered or unexported fields

HostAndPort is an immutable representation of a host and port.

func FromHost

func FromHost(host string) (HostAndPort, error)

FromHost builds a HostAndPort instance from a host only.

Note: Non-bracketed IPv6 literals are allowed. Use requireBracketsForIPv6() to prohibit these.

func FromParts

func FromParts(host string, port int) (HostAndPort, error)

FromParts builds a HostAndPort instance from separate host and port values.

Note: Non-bracketed IPv6 literals are allowed. Use requireBracketsForIPv6() to prohibit these.

func FromString

func FromString(hostPort string) (HostAndPort, error)

FromString splits a freeform string into a host and port, without strict validation.

Note that the host-only formats will leave the port field undefined. You can use withDefaultPort(int) to patch in a default value.

func (HostAndPort) GetPortOrDefault

func (hp HostAndPort) GetPortOrDefault(defaultPort int) int

GetPortOrDefault returns the current port number, with a default if no port is defined.

func (HostAndPort) HasPort

func (hp HostAndPort) HasPort() bool

HasPort returns true if this instance has a defined port.

func (HostAndPort) Host

func (hp HostAndPort) Host() string

Host returns the portion of this HostAndPort instance that should represent the hostname or IPv4/IPv6 literal.

A successful parse does not imply any degree of sanity in this field.

func (HostAndPort) Port

func (hp HostAndPort) Port() (int, error)

Port gets the current port number, failing if no port is defined.

func (HostAndPort) RequireBracketsForIPv6

func (hp HostAndPort) RequireBracketsForIPv6() (HostAndPort, error)

RequireBracketsForIPv6 generate an error if the host might be a non-bracketed IPv6 literal.

Use this call after fromString(String) to increase the strictness of the parser, and disallow IPv6 literals that don't contain these brackets.

Note that this parser identifies IPv6 literals solely based on the presence of a colon.

func (HostAndPort) String

func (hp HostAndPort) String() string

String rebuilds the host:port string, including brackets if necessary.

func (HostAndPort) WithDefaultPort

func (hp HostAndPort) WithDefaultPort(defaultPort int) (HostAndPort, error)

WithDefaultPort provides a default port if the parsed string contained only a host.

You can use this after fromString(String) to include a port in case the port was omitted from the input string. If a port was already provided, then this method is a no-op.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL