IfInfo
To create a program in Go that lists all available network interfaces and their IP addresses, you need to create a few files: main.go
for the main logic, and optionally, a interfaces.go
file for the interface-handling functionality. Here is a step-by-step guide:
Step 1: Initialize the Go Module
First, create a new directory for your project and initialize the Go module.
Step 2: Create the interfaces.go
File
Create a lib/interfaces.go
file in to handle the functionality of listing network interfaces.
Step 3: Create the main.go
File
Create a main.go
file to use the network interface listing functionality.
Step 4: Run the Program
Run the program using the go run
command.
The program will retrieve and display:
All available network interfaces on the system.
The associated IP addresses for each interface.
Example Output
For a machine with several interfaces:
Notes
The program uses the
net
package, which provides tools to list interfaces and their details.IP addresses include both IPv4 and IPv6, displayed with their subnet masks.
If an interface has no assigned IP address, it will still be listed, but without any associated address output.
Extensions
You can modify the program to filter interfaces (e.g., only show interfaces with an active connection or only display IPv4 addresses).
Add error handling to improve robustness in environments with unusual network configurations.