Documentation
¶
Overview ¶
Package whatsmyip provides functionality to determine the external IP address of the machine.
This package uses multiple online services to fetch the IP address, improving reliability and reducing dependency on any single service. It employs concurrent requests and returns the first successful response, cancelling other ongoing requests.
The main function of this package is Get(), which returns the external IP address. The package also includes internal utilities for logging.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Get ¶
Get fetches the external IP address of the machine by concurrently querying multiple URLs.
The function performs the following steps: 1. Creates a cancellable context 2. Shuffles the list of URLs to randomize the order of requests 3. Concurrently sends HTTP GET requests to all URLs 4. Returns the first successfully retrieved IP address 5. Cancels all ongoing requests once a successful response is received
If all requests fail, it returns an error.
Return values:
- ip: The retrieved external IP address (empty string if all requests fail)
- url: The URL that successfully provided the IP address (empty string if all requests fail)
- err: Error if all requests fail, nil otherwise
The function uses the APP_ENV environment variable to determine the log level. It logs debug information for successful fetches and an error if all requests fail.
This function is designed to be resilient, fast, and to reduce load on any single IP lookup service.
Example ¶
ip, url, err := Get() if err != nil { fmt.Printf("Error: %v\n", err) panic(err.Error()) } fmt.Printf("Your IP address is: %s\n", ip) fmt.Printf("Retrieved from: %s\n", url)
Output:
Types ¶
This section is empty.