Documentation
¶
Overview ¶
Package network provides value objects related to network information.
This package contains value objects that represent network-related concepts such as IP addresses and URLs. These value objects are immutable and follow the Value Object pattern from Domain-Driven Design.
Key value objects in this package:
- IPAddress: Represents an IP address (IPv4 or IPv6)
- URL: Represents a URL with validation and parsing capabilities
The IPAddress value object provides methods for:
- Creating and validating IP addresses
- String representation
- Equality comparison
- Checking IP address properties (IPv4, IPv6, loopback, private)
- JSON marshaling
The URL value object provides methods for:
- Creating and validating URLs
- String representation
- Equality comparison
- Extracting URL components (domain, path, query)
- Validation of scheme and host
Example usage:
// Create a new IP address
ip, err := network.NewIPAddress("192.168.1.1")
if err != nil {
// Handle validation error
}
// Check IP address properties
if ip.IsIPv4() {
// Handle IPv4 address
}
if ip.IsPrivate() {
// Handle private IP address
}
// Create a new URL
url, err := network.NewURL("https://example.com/path?query=value")
if err != nil {
// Handle validation error
}
// Extract URL components
domain, err := url.Domain()
if err != nil {
// Handle error
}
path, err := url.Path()
if err != nil {
// Handle error
}
All value objects in this package are designed to be immutable, so they cannot be changed after creation. To modify a value object, create a new instance with the desired values.
Package network provides value objects related to network information.
Package network provides value objects related to network information.
Index ¶
- type IPAddress
- func (ip IPAddress) Equals(other IPAddress) bool
- func (ip IPAddress) IsEmpty() bool
- func (ip IPAddress) IsIPv4() bool
- func (ip IPAddress) IsIPv6() bool
- func (ip IPAddress) IsLoopback() bool
- func (ip IPAddress) IsPrivate() bool
- func (ip IPAddress) MarshalJSON() ([]byte, error)
- func (ip IPAddress) String() string
- func (ip IPAddress) Validate() error
- type URL
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type IPAddress ¶
type IPAddress string
IPAddress represents an IP address value object that supports both IPv4 and IPv6 formats
func NewIPAddress ¶
NewIPAddress creates a new IPAddress with validation for both IPv4 and IPv6 formats It uses Go's net.ParseIP function which supports both IPv4 (e.g., "192.168.1.1") and IPv6 (e.g., "2001:db8::1") address formats
func (IPAddress) Equals ¶
Equals checks if two IPAddresses are equal Properly handles comparison of both IPv4 and IPv6 addresses in different representations (e.g., IPv4 mapped to IPv6, or different IPv6 notations for the same address)
func (IPAddress) IsEmpty ¶
IsEmpty checks if the IPAddress is empty An IPAddress is considered empty only if it's an empty string Invalid IP addresses are not considered empty
func (IPAddress) IsIPv4 ¶
IsIPv4 checks if the IP address is in IPv4 format (e.g., "192.168.1.1") Returns true only for valid IPv4 addresses, false for IPv6 addresses or invalid formats
func (IPAddress) IsIPv6 ¶
IsIPv6 checks if the IP address is in IPv6 format (e.g., "2001:db8::1") Returns true only for valid IPv6 addresses, false for IPv4 addresses or invalid formats
func (IPAddress) IsLoopback ¶
IsLoopback checks if the IP address is a loopback address Works with both IPv4 loopback (127.0.0.0/8) and IPv6 loopback (::1) addresses
func (IPAddress) IsPrivate ¶
IsPrivate checks if the IP address is in a private range Supports both IPv4 private ranges (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) and IPv6 private ranges (fc00::/7)
func (IPAddress) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface