Documentation
¶
Overview ¶
Package dns provides DNS lookup functionality with built-in caching support.
The package offers a simple API for resolving hostnames to IP addresses with automatic caching to improve performance and reduce DNS query load. It handles both raw hostnames and URLs, automatically extracting the hostname portion when needed.
Features:
- Automatic caching with 1-hour TTL
- Support for both hostnames and URLs
- IPv4 address filtering
- Direct IP address passthrough
Basic Usage:
// Lookup with caching (recommended)
ips, err := dns.CacheLookup("A", "example.com")
if err != nil {
log.Fatal(err)
}
for _, ip := range ips {
fmt.Printf("IP: %s\n", ip)
}
// Direct lookup without caching
ips, err := dns.Lookup("A", "https://example.com")
// Automatically extracts hostname from URL
The package prioritizes IPv4 addresses in results and handles IP addresses directly without performing DNS lookups.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CacheLookup ¶
CacheLookup performs a DNS lookup with caching support. Results are cached for 1 hour to reduce DNS query load.
Parameters:
- recordType: DNS record type (e.g., "A", "AAAA"). Currently not used but reserved for future use.
- hostname: The hostname, URL, or IP address to resolve
Returns:
- []net.IP: Slice of resolved IPv4 addresses
- error: If the lookup fails or hostname is invalid
Example:
ips, err := CacheLookup("A", "google.com")
if err != nil {
return err
}
fmt.Printf("Resolved IPs: %v\n", ips)
func Lookup ¶
Lookup performs a direct DNS lookup without caching. It uses Go's default resolver to query DNS servers.
The function handles multiple input formats:
- Raw hostnames: "example.com"
- URLs: "https://example.com:8080/path" (extracts hostname)
- IP addresses: "192.168.1.1" (returns immediately without lookup)
Only IPv4 addresses are returned in the result.
Parameters:
- recordType: DNS record type (currently not used but reserved for future use)
- hostname: The hostname, URL, or IP address to resolve
Returns:
- []net.IP: Slice of resolved IPv4 addresses
- error: If the lookup fails or hostname is invalid
Types ¶
This section is empty.