Extnetip
Package extnetip extends the stdlib with a few missing but important auxiliary functions for converting IP-prefixes to IP-ranges and vice versa.
Install / Use
/learn @gaissmai/ExtnetipREADME
package extnetip
Package extnetip is an extension to the Go standard library package net/netip, providing
a set of important auxiliary functions currently missing from netip for
converting IP prefixes to IP ranges and vice versa.
With these extensions, it becomes straightforward to build third-party IP-range
libraries based on net/netip.
API
import "github.com/gaissmai/extnetip"
func Range(p netip.Prefix) (first, last netip.Addr)
func Prefix(first, last netip.Addr) (prefix netip.Prefix, ok bool)
func CommonPrefix(pfx1, pfx2 netip.Prefix) (pfx netip.Prefix)
func All(first, last netip.Addr) iter.Seq[netip.Prefix]
Unsafe Mode
This package supports two modes of operation for converting between netip.Addr and
a custom internal uint128 representation:
-
When built with the
unsafebuild tag (e.g.,go build -tags=unsafe), conversions useunsafe.Pointerto perform zero-copy, direct memory reinterpretation. This method is significantly faster. -
Without the
unsafebuild tag, conversions are performed safely by usingbinary.ByteOrder-based byte slice manipulations, avoiding use of theunsafepackage. This is the default mode and is suitable when importing unsafe modules is prohibited.
Performance Benchmark
Below is a benchmark comparing the safe (default) and unsafe conversion methods:
goos: linux
goarch: amd64
pkg: github.com/gaissmai/extnetip
cpu: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
│ safe.bm │ unsafe.bm │
│ sec/op │ sec/op vs base │
Range/v4-8 12.435n ± 0% 5.099n ± 1% -58.99% (p=0.002 n=6)
Range/v6-8 25.855n ± 0% 5.399n ± 0% -79.12% (p=0.002 n=6)
Prefix/v4-8 16.31n ± 0% 12.07n ± 1% -25.99% (p=0.002 n=6)
Prefix/v6-8 17.38n ± 1% 10.88n ± 1% -37.45% (p=0.002 n=6)
CommonPrefix/v4-8 26.79n ± 0% 21.58n ± 1% -19.43% (p=0.002 n=6)
CommonPrefix/v6-8 28.25n ± 0% 21.00n ± 1% -25.63% (p=0.002 n=6)
geomean 20.25n 10.86n -46.38%
Future Work
It is hoped that these frequently needed helper functions will be added to the Go standard
library's netip package at some point in the future by the maintainers.
Until then, extnetip provides a robust and efficient alternative.
Related Skills
node-connect
346.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
107.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
346.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
346.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
