macaddr

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: Apache-2.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

MAC Address Utility Package

The macaddr package provides utilities for handling and manipulating MAC addresses in various formats, supporting the most common styles (colon, hyphen, dot, and no separators). The primary purpose is to provide a consistent way to handle MAC addresses throughout the application.

Features

  • Format Conversion: Convert between different MAC address formats
  • Validation: Validate MAC addresses in various formats
  • Normalization: Normalize MAC addresses to a consistent format (lowercase, no separators)
  • Format Detection: Detect the format of a MAC address
  • Equality Comparison: Compare MAC addresses regardless of format

Supported Formats

The package supports the following MAC address formats:

Format Example Constant
No separators 001122334455 FormatNone
Colon-separated 00:11:22:33:44:55 FormatColon
Hyphen-separated 00-11-22-33-44-55 FormatHyphen
Dot-separated (Cisco) 0011.2233.4455 FormatDot

Usage Examples

Normalize a MAC Address
// Convert any MAC format to the standard normalized form (lowercase, no separators)
normalizedMac, err := macaddr.Normalize("00:11:22:AA:BB:CC")
// normalizedMac = "001122aabbcc"

// If you're certain the MAC is valid or prefer empty strings over errors
normalizedMac := macaddr.NormalizeOrEmpty("00:11:22:AA:BB:CC")
Format a MAC Address
// Convert to colon format
formattedMac, err := macaddr.Format("001122aabbcc", macaddr.FormatColon)
// formattedMac = "00:11:22:aa:bb:cc"

// Convert to hyphen format
formattedMac, err := macaddr.Format("00:11:22:aa:bb:cc", macaddr.FormatHyphen)
// formattedMac = "00-11-22-aa-bb-cc"

// Convert to dot format (Cisco)
formattedMac, err := macaddr.Format("00:11:22:aa:bb:cc", macaddr.FormatDot)
// formattedMac = "0011.22aa.bbcc"
Validate a MAC Address
// Check if a string is a valid MAC in any supported format
isValid := macaddr.IsValid("00:11:22:33:44:55")  // true
isValid := macaddr.IsValid("001122334455")       // true
isValid := macaddr.IsValid("invalid")            // false

// Check if a MAC is in a specific format
isColonFormat := macaddr.IsValidWithFormat("00:11:22:33:44:55", macaddr.FormatColon)  // true
isColonFormat := macaddr.IsValidWithFormat("00-11-22-33-44-55", macaddr.FormatColon)  // false
Compare MAC Addresses
// Compare MAC addresses regardless of format
areEqual := macaddr.Equal("00:11:22:33:44:55", "00-11-22-33-44-55")  // true
areEqual := macaddr.Equal("00:11:22:33:44:55", "001122334455")       // true
areEqual := macaddr.Equal("00:11:22:33:44:55", "00:11:22:33:44:56")  // false
Detect MAC Address Format
format, err := macaddr.DetectFormat("00:11:22:33:44:55")
// format = macaddr.FormatColon

format, err := macaddr.DetectFormat("00-11-22-33-44-55")
// format = macaddr.FormatHyphen

format, err := macaddr.DetectFormat("0011.2233.4455")
// format = macaddr.FormatDot

format, err := macaddr.DetectFormat("001122334455")
// format = macaddr.FormatNone

Error Handling

The package provides proper error handling for invalid MAC addresses:

// All functions that might fail return meaningful errors
_, err := macaddr.Normalize("invalid")
if err != nil {
    // Handle error: err = macaddr.ErrInvalidMAC
}

Documentation

Overview

Package macaddr provides utilities for handling and manipulating MAC addresses

This package offers a set of functions to validate, normalize, format, and compare MAC addresses in various formats, supporting the most common styles (colon, hyphen, dot, and no separators). The primary purpose is to provide a consistent way to handle MAC addresses throughout the application.

The default normalized format used by the application for storage and lookups is lowercase with no separators.

Index

Constants

View Source
const (
	// FormatNone represents a MAC address with no separators (aabbccddeeff)
	FormatNone = iota
	// FormatColon represents a MAC address with colon separators (aa:bb:cc:dd:ee:ff)
	FormatColon
	// FormatHyphen represents a MAC address with hyphen separators (aa-bb-cc-dd-ee-ff)
	FormatHyphen
	// FormatDot represents a MAC address with dot separators (aabb.ccdd.eeff)
	FormatDot
)

Format constants represent different MAC address formats

Variables

View Source
var (
	// ErrInvalidMAC indicates that the provided string is not a valid MAC address
	ErrInvalidMAC = errors.New("invalid MAC address")
)

Functions

func DetectFormat

func DetectFormat(mac string) (int, error)

DetectFormat tries to detect the format of a MAC address. This function returns the format constant (FormatNone, FormatColon, FormatHyphen, FormatDot) that matches the format of the input MAC address.

Returns an error if the MAC address is invalid. Returns the format constant and nil error if successful.

Examples:

DetectFormat("001122334455")      -> FormatNone, nil
DetectFormat("00:11:22:33:44:55") -> FormatColon, nil
DetectFormat("invalid")           -> -1, ErrInvalidMAC

func Equal

func Equal(mac1, mac2 string) bool

Equal checks if two MAC addresses are equal, ignoring format differences. This function normalizes both MAC addresses (converts to lowercase with no separators) before comparing them. If either MAC address is invalid, they are considered not equal.

Examples:

Equal("00:11:22:33:44:55", "00-11-22-33-44-55") -> true
Equal("00:11:22:33:44:55", "00:11:22:33:44:56") -> false
Equal("00:11:22:33:44:55", "invalid")           -> false

This is useful for comparing MAC addresses that might be in different formats.

func Format

func Format(mac string, format int) (string, error)

Format converts a MAC address into the specified format. The input MAC address can be in any valid format; it will be normalized first, then converted to the requested format. The format parameter must be one of the predefined Format constants (FormatNone, FormatColon, FormatHyphen, FormatDot).

Examples:

Format("00:11:22:33:44:55", FormatNone)   -> "001122334455"
Format("001122334455", FormatColon)       -> "00:11:22:33:44:55"
Format("00:11:22:33:44:55", FormatHyphen) -> "00-11-22-33-44-55"
Format("00-11-22-33-44-55", FormatDot)    -> "0011.2233.4455"

Returns an error if the input MAC address is invalid or if an unsupported format is requested.

func IsValid

func IsValid(mac string) bool

IsValid checks if a string is a valid MAC address in any of the common formats. This function recognizes the following formats: - No separators: 001122334455 - Colon-separated: 00:11:22:33:44:55 - Hyphen-separated: 00-11-22-33-44-55 - Dot-separated (Cisco format): 0011.2233.4455

MAC addresses with mixed separators are NOT considered valid. Empty strings are considered invalid.

Returns true if the MAC address is valid in any of the supported formats, false otherwise.

func IsValidWithFormat

func IsValidWithFormat(mac string, format int) bool

IsValidWithFormat checks if a MAC address is valid with a specific format. The format parameter must be one of the predefined Format constants (FormatNone, FormatColon, FormatHyphen, FormatDot).

Examples:

IsValidWithFormat("001122334455", FormatNone)       -> true
IsValidWithFormat("00:11:22:33:44:55", FormatColon) -> true
IsValidWithFormat("00:11:22:33:44:55", FormatNone)  -> false

Returns false for empty strings or if an unsupported format is specified.

func MustNormalize

func MustNormalize(mac string) string

MustNormalize is like Normalize but panics if the MAC address is invalid. This function is useful when you are certain that the MAC address is valid, and want to avoid error handling.

Warning: This function panics if the input is not a valid MAC address. Use Normalize or NormalizeOrEmpty for safer alternatives.

func Normalize

func Normalize(mac string) (string, error)

Normalize converts any valid MAC address into a normalized form (lowercase, no separators). This is the default format used by the application for storage and lookups. It accepts MAC addresses in various formats (colon, hyphen, dot, or no separators) and returns the normalized form. If the input is not a valid MAC address, it returns an empty string and an error.

Examples:

"00:11:22:33:44:55" -> "001122334455"
"00-11-22-33-44-55" -> "001122334455"
"0011.2233.4455"    -> "001122334455"
"001122334455"      -> "001122334455"

func NormalizeFast

func NormalizeFast(mac string) string

NormalizeFast normalizes a MAC address without validation. This function is optimized for internal use where the MAC address is already known to be valid (e.g., from cache data that was previously validated).

It converts to lowercase and removes all common separators (colons, hyphens, dots, and spaces). Unlike Normalize, it does NOT validate the input format.

Use this function only for trusted internal data (cache indexing/lookups). For external input (API responses, user config), use Normalize instead.

Examples:

"00:11:22:33:44:55" -> "001122334455"
"00-11-22-33-44-55" -> "001122334455"
"invalid"           -> "invalid" (no validation!)
""                  -> ""

func NormalizeOrEmpty

func NormalizeOrEmpty(mac string) string

NormalizeOrEmpty normalizes a MAC address or returns an empty string if invalid. This is a safe alternative to Normalize when you prefer to handle invalid MAC addresses by returning an empty string rather than an error.

Examples:

"00:11:22:33:44:55" -> "001122334455"
"invalid"           -> ""
""                  -> ""

Types

This section is empty.

Jump to

Keyboard shortcuts

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