netiputil

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: BSD-3-Clause Imports: 5 Imported by: 0

README

netiputil

A tiny Go utility package for a more relaxed representation of netip.AddrPort and netip.Addr datatypes. Documentation

Features

This library is an easy wrapper to parse IP addresses from configurations or other sources, where more relaxed formats may occur. Key differences from the standard library netip parsers are:

  • Empty strings are treated as zero values
  • Bracketed IPv6 addresses for netip.Addr are accepted
  • Omits port numbers for netip.AddrPort when .Port() == 0
  • Omits address for netip.AddrPort when .Addr() == netip.Addr{}

Usage

import "github.com/herczegzsolt/netiputil"

// Parse flexible address formats
addr, _ := netiputil.StringAddr("")              // netip.Addr{}
addr, _ := netiputil.StringAddr("[2001:db8::1]") // netip.MustParseAddr("2001:db8::1")

// Parse flexible address:port formats
addrPort, _ := netiputil.StringAddrPort(":8080")           // netip.AddrPortFrom(netip.Addr{}, 8080)
addrPort, _ := netiputil.StringAddrPort("192.0.2.1")       // netip.MustParseAddrPort("192.0.2.1:0")
addrPort, _ := netiputil.StringAddrPort("192.0.2.1:8080")  // netip.MustParseAddrPort("192.0.2.1:8080")

// Format with relaxed output (empty strings for zero values)
s := netiputil.AddrString(netip.Addr{})     // ""
s := netiputil.AddrPortString(netip.MustParseAddrPort("[2001:db8::1]:0"))     // 2001:db8::1
s := netiputil.PortString(0)                // ""

github.com/goccy/go-yaml integration

This module has support for the custom marshalers of github.com/goccy/go-yaml. To override the yaml marshaling behavior of netip.Addr and netip.AddrPort, do a blank import of github.com/herczegzsolt/netiputil/yaml.

import _ "github.com/herczegzsolt/netiputil/yaml"
import yaml "github.com/goccy/go-yaml"

type A struct {
	B netip.AddrPort
}

var a A
yaml.Unmarshal([]byte(`{B: ":1337"}`), &a) // a.B == netip.AddrPortFrom(netip.Addr{}, 1337)

Dependencies

Core functionality of this module (netiputil package) does only depends on the go standard library. The yaml package depends on the github.com/goccy/go-yaml for the purpose of registering custom marshalers.

Documentation

Overview

Package netiputil is a tiny utility package for a more relaxed representation of netip.AddrPort and netip.Addr datatypes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddrPortString

func AddrPortString(addrp netip.AddrPort) string

AddrPortString returns the string form of the IP address addrp. It returns one of 5 forms, if port is zero:

  • "", if addrp is the zero netip.AddrPort
  • ":1337", if addrp.Addr() is the zero netip.Addr
  • "192.0.2.1"
  • "192.0.2.1:1337"
  • "2001:db8::1"
  • "[2001:db8::1]:1337"
  • "::ffff:192.0.2.1"
  • "[::ffff:192.0.2.1]:1337"
  • "fe80:db8::1%eth0"
  • "[fe80:db8::1%eth0]:1337"

func AddrString

func AddrString(addr netip.Addr) string

AddrString returns the string form of the IP address addr. It returns one of 5 forms:

  • "", if addrp is the zero netip.Addr
  • "192.0.2.1"
  • "2001:db8::1"
  • "::ffff:192.0.2.1"
  • "fe80:db8::1%eth0"

Note that unlike the netip package, the zero addr is represented as an empty string.

func PortString

func PortString(port uint16) string

PortString returns the string form of the port. It is either the empty string if the port is zero, or base 10 representation.

func StringAddr

func StringAddr(s string) (netip.Addr, error)

StringAddr parses a string into a netip.Addr. It accepts either of the following formats:

  • "", for the zero netip.Addr
  • "192.0.2.1"
  • "2001:db8::1"
  • "[2001:db8::1]"
  • "::ffff:192.0.2.1"
  • "[::ffff:192.0.2.1]"
  • "fe80:db8::1%eth0"
  • "[fe80:db8::1%eth0]"

func StringAddrPort

func StringAddrPort(s string) (netip.AddrPort, error)

StringAddrPort parses a string into a netip.AddrPort. It accepts either of the following formats:

  • "", ":" or ":0", for the zero netip.AddrPort
  • ":1337", for the zero netip.AddrPort with port 1337
  • "192.0.2.1"
  • "192.0.2.1:1337"
  • "2001:db8::1"
  • "[2001:db8::1]"
  • "[2001:db8::1]:1337"
  • "::ffff:192.0.2.1"
  • "[::ffff:192.0.2.1]"
  • "[::ffff:192.0.2.1]:1337"
  • "fe80:db8::1%eth0"
  • "[fe80:db8::1%eth0]"
  • "[fe80:db8::1%eth0]:1337"

func StringPort

func StringPort(s string) (uint16, error)

StringPort parses a string into a uint16 port number. It accepts "" or "0" for the zero port, or base 10 representation.

Types

This section is empty.

Directories

Path Synopsis
Package yaml does not provide any public identifiers.
Package yaml does not provide any public identifiers.

Jump to

Keyboard shortcuts

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