net

package
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2024 License: MIT Imports: 3 Imported by: 0

README

goutils/v2/net

The net package is a collection of utility functions designed to simplify common net tasks.


Table of contents


Functions

DownloadFile(string, string)
DownloadFile(string, string) string, error

DownloadFile downloads a file from the provided URL and saves it to the specified location on the local filesystem. The function takes the source URL and the destination path as inputs and returns the path where the file was saved or an error.

Parameters:

url: A string representing the URL of the file to be downloaded. dest: A string representing the destination path where the file should be saved on the local filesystem.

Returns:

string: The path where the downloaded file was saved. error: An error if the function fails to download the file.


PublicIP(uint)
PublicIP(uint) string, error

PublicIP uses several external services to get the public IP address of the system running it, using github.com/GlenDC/go-external-ip. The function takes an IP protocol version (4 or 6) as input and returns the public IP address as a string or an error.

Parameters:

protocol: A uint representing the IP protocol version (4 or 6).

Returns:

string: The public IP address of the system in string format. error: An error if the function fails to retrieve the public IP address.


Installation

To use the goutils/v2/net package, you first need to install it. Follow the steps below to install via go get.

go get github.com/l50/goutils/v2/net

Usage

After installation, you can import the package in your Go project using the following import statement:

import "github.com/l50/goutils/v2/net"

Tests

To ensure the package is working correctly, run the following command to execute the tests for goutils/v2/net:

go test -v

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.


License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DownloadFile

func DownloadFile(url string, dest string) (string, error)

DownloadFile downloads a file from the provided URL and saves it to the specified location on the local filesystem. The function takes the source URL and the destination path as inputs and returns the path where the file was saved or an error.

Parameters:

url: A string representing the URL of the file to be downloaded. dest: A string representing the destination path where the file should be saved on the local filesystem.

Returns:

string: The path where the downloaded file was saved. error: An error if the function fails to download the file.

Example
package main

import (
	"log"

	netutils "github.com/l50/goutils/v2/net"
)

func main() {
	url := "http://example.com/path/to/file"
	dest := "/path/to/save/location"
	file, err := netutils.DownloadFile(url, dest)

	if err != nil {
		log.Fatalf("failed to download file: %v", err)
	}
	_ = file
}
Output:

func PublicIP

func PublicIP(protocol uint) (string, error)

PublicIP uses several external services to get the public IP address of the system running it, using github.com/GlenDC/go-external-ip. The function takes an IP protocol version (4 or 6) as input and returns the public IP address as a string or an error.

Parameters:

protocol: A uint representing the IP protocol version (4 or 6).

Returns:

string: The public IP address of the system in string format. error: An error if the function fails to retrieve the public IP address.

Example
package main

import (
	"log"
	"net"

	netutils "github.com/l50/goutils/v2/net"
)

func main() {
	protocol := uint(4) // or 6 for IPv6
	ip, err := netutils.PublicIP(protocol)
	if err != nil {
		log.Fatalf("failed to get public IP address: %v", err)
	}

	if net.ParseIP(ip) == nil {
		log.Fatal("invalid IP address received")
	}

	_ = ip
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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