This recipe explains how to retrieve IP addresses from available local interfaces.
Resolving local IP addresses
How to do it...
- Open the console and create the folder chapter07/recipe01.
- Navigate to the directory.
- Create the interfaces.go file with the following content:
package main
import (
"fmt"
"net"
)
func main() {
// Get all network interfaces
interfaces, err := net.Interfaces()
if err != nil {
panic(err)
}
for _, interf := range interfaces {
// Resolve addresses
// for each interface
addrs, err := interf.Addrs()
if err != nil {
panic(err...