go

module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2025 License: Apache-2.0

README

IPFreely.uk

IP address manipulation library written in Go. This library treats IP addresses as generic unsigned integers capable of arithmetic and bitwise operations and includes a few discrete set collection types. This library does not perform network I/O and imposes no transitive runtime dependencies beyond the standard library.

Example

package main

import(
    "fmt"

    "github.com/ipfreely-up/go/ip"
    "github.com/ipfreely-up/go/ipmask"
)

func main() {
	network4 := ip.V4().MustFromBytes(192, 0, 2, 128)
	printNetworkDetails(network4, 26)

	println()

	network6 := ip.MustParse(ip.V6(), "2001:DB80::")
	printNetworkDetails(network6, 65)
}

func printNetworkDetails[A ip.Int[A]](network A, maskBits int) {
	fam := network.Family()
	mask := ipmask.For(fam, maskBits)
	maskComplement := mask.Not()

	zero := fam.FromInt(0)
	if !ip.Eq(mask.And(maskComplement), zero) {
		panic("Mask does not cover network address")
	}

	println("First Address:", network.String())
	println("Last Address:", network.Or(maskComplement).String())
	println("Mask:", mask.String())
	fmt.Printf("CIDR Notation: %s/%d\n", network.String(), maskBits)
}

Output:

First Address: 192.0.2.128
Last Address: 192.0.2.191
Mask: 255.255.255.192
CIDR Notation: 192.0.2.128/26

First Address: 2001:db80::
Last Address: 2001:db80::7fff:ffff:ffff:ffff
Mask: ffff:ffff:ffff:ffff:8000::
CIDR Notation: 2001:db80::/65

Module

The module name is github.com/ipfreely-uk/go. Add a require statement to go.mod to utilise.

Packages

Package Purpose
import github.com/ipfreely-uk/go/ip IP addresses as integers
import github.com/ipfreely-uk/go/ipmask IP subnet mask funcs
import github.com/ipfreely-uk/go/ipset IP address discrete sets
import github.com/ipfreely-uk/go/ipstd Convert to/from stdlib

Versus Standard Library

Selective comparison with standard library types in netip.

Feature IPFreely.uk netip
Immutable Types Y Y
Categorization Y
Generic Types Y
Arithmetic/Bitwise Ops Y
IPv6 Zones Y
CIDR Blocks Y Y
Arbitrary Ranges/Sets Y
Iteration Y

Continuous Integration

Go Go Reference

Directories

Path Synopsis
IP addresses as generic, immutable positive integers.
IP addresses as generic, immutable positive integers.
IP subnet mask functions.
IP subnet mask functions.
Discrete sets and ranges of IP addresses.
Discrete sets and ranges of IP addresses.
Convenience functions to convert to & from standard library types.
Convenience functions to convert to & from standard library types.
This package is used in example code and does not form part of the library.
This package is used in example code and does not form part of the library.

Jump to

Keyboard shortcuts

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