Skip to main content

Overview

The Interfaces function retrieves detailed information about all network interfaces installed on the system, including physical and virtual interfaces.

Functions

func Interfaces() (InterfaceStatList, error)
func InterfacesWithContext(ctx context.Context) (InterfaceStatList, error)

Parameters

ctx
context.Context
Context for cancellation and timeout control

Returns

Returns an InterfaceStatList (slice of InterfaceStat) containing information about each network interface.

InterfaceStat Structure

The InterfaceStat struct contains comprehensive information about a network interface.
type InterfaceStat struct {
    Index        int               // Interface index
    MTU          int               // Maximum transmission unit
    Name         string            // Interface name (e.g., "en0", "eth0", "wlan0")
    HardwareAddr string            // MAC address (IEEE MAC-48, EUI-48, EUI-64)
    Flags        []string          // Interface flags
    Addrs        InterfaceAddrList // List of addresses assigned to interface
}

Fields

Index
int
The interface index, a unique identifier for the interface.
MTU
int
Maximum Transmission Unit - the maximum size of a packet that can be transmitted through this interface (in bytes).
Name
string
The interface name. Examples:
  • Linux: "eth0", "wlan0", "lo"
  • macOS: "en0", "lo0", "awdl0"
  • Windows: "Ethernet", "Wi-Fi"
HardwareAddr
string
Hardware (MAC) address in IEEE MAC-48, EUI-48, or EUI-64 format (e.g., "00:11:22:33:44:55").
Flags
[]string
List of interface flags indicating the interface state and capabilities. Common flags:
  • "up" - Interface is administratively up
  • "broadcast" - Interface supports broadcast
  • "loopback" - Loopback interface
  • "pointtopoint" - Point-to-point link
  • "multicast" - Interface supports multicast
Addrs
InterfaceAddrList
List of IP addresses assigned to this interface.

InterfaceAddr Structure

The InterfaceAddr struct represents an IP address assigned to an interface.
type InterfaceAddr struct {
    Addr string // IP address with subnet mask (e.g., "192.168.1.100/24")
}
Addr
string
IP address in CIDR notation, including the subnet mask (e.g., "192.168.1.100/24", "fe80::1/64").

Type Aliases

type InterfaceStatList []InterfaceStat
type InterfaceAddrList []InterfaceAddr
Both types include a String() method for JSON serialization.

Usage Examples

List All Interfaces

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/net"
)

func main() {
    interfaces, err := net.Interfaces()
    if err != nil {
        panic(err)
    }
    
    fmt.Printf("Found %d network interfaces\n\n", len(interfaces))
    
    for _, iface := range interfaces {
        fmt.Printf("Interface: %s\n", iface.Name)
        fmt.Printf("  Index: %d\n", iface.Index)
        fmt.Printf("  MTU: %d\n", iface.MTU)
        fmt.Printf("  MAC: %s\n", iface.HardwareAddr)
        fmt.Printf("  Flags: %v\n", iface.Flags)
        fmt.Printf("  Addresses: %d\n\n", len(iface.Addrs))
    }
}

Get Active Interfaces

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/net"
)

func main() {
    interfaces, err := net.Interfaces()
    if err != nil {
        panic(err)
    }
    
    fmt.Println("Active interfaces:")
    
    for _, iface := range interfaces {
        // Check if interface is up
        isUp := false
        for _, flag := range iface.Flags {
            if flag == "up" {
                isUp = true
                break
            }
        }
        
        if isUp {
            fmt.Printf("  %s (MAC: %s)\n", iface.Name, iface.HardwareAddr)
        }
    }
}

Display Interface Addresses

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/net"
)

func main() {
    interfaces, err := net.Interfaces()
    if err != nil {
        panic(err)
    }
    
    for _, iface := range interfaces {
        if len(iface.Addrs) == 0 {
            continue
        }
        
        fmt.Printf("%s:\n", iface.Name)
        for _, addr := range iface.Addrs {
            fmt.Printf("  %s\n", addr.Addr)
        }
        fmt.Println()
    }
}

Find Interface by Name

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/net"
)

func findInterface(name string) (*net.InterfaceStat, error) {
    interfaces, err := net.Interfaces()
    if err != nil {
        return nil, err
    }
    
    for _, iface := range interfaces {
        if iface.Name == name {
            return &iface, nil
        }
    }
    
    return nil, fmt.Errorf("interface %s not found", name)
}

func main() {
    iface, err := findInterface("eth0")
    if err != nil {
        fmt.Println(err)
        return
    }
    
    fmt.Printf("Found interface %s\n", iface.Name)
    fmt.Printf("  MAC: %s\n", iface.HardwareAddr)
    fmt.Printf("  MTU: %d\n", iface.MTU)
}

Check Interface Capabilities

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/net"
)

func hasFlag(flags []string, flag string) bool {
    for _, f := range flags {
        if f == flag {
            return true
        }
    }
    return false
}

func main() {
    interfaces, err := net.Interfaces()
    if err != nil {
        panic(err)
    }
    
    for _, iface := range interfaces {
        fmt.Printf("%s:\n", iface.Name)
        fmt.Printf("  Up: %v\n", hasFlag(iface.Flags, "up"))
        fmt.Printf("  Loopback: %v\n", hasFlag(iface.Flags, "loopback"))
        fmt.Printf("  Broadcast: %v\n", hasFlag(iface.Flags, "broadcast"))
        fmt.Printf("  Multicast: %v\n", hasFlag(iface.Flags, "multicast"))
        fmt.Println()
    }
}

Using Context

package main

import (
    "context"
    "fmt"
    "time"
    "github.com/shirou/gopsutil/v4/net"
)

func main() {
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()
    
    interfaces, err := net.InterfacesWithContext(ctx)
    if err != nil {
        panic(err)
    }
    
    for _, iface := range interfaces {
        fmt.Printf("%s: %s\n", iface.Name, iface.HardwareAddr)
    }
}

Get Non-Loopback Interfaces

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/net"
)

func main() {
    interfaces, err := net.Interfaces()
    if err != nil {
        panic(err)
    }
    
    fmt.Println("Non-loopback interfaces:")
    
    for _, iface := range interfaces {
        isLoopback := false
        for _, flag := range iface.Flags {
            if flag == "loopback" {
                isLoopback = true
                break
            }
        }
        
        if !isLoopback {
            fmt.Printf("  %s\n", iface.Name)
        }
    }
}

Interface Flags

Common interface flags returned in the Flags field:
Interface is administratively up and operational.
Interface supports broadcast addressing (can send packets to all devices on the network).
Loopback interface (typically lo or lo0), used for local communication.
Point-to-point link (e.g., PPP, VPN connections).
Interface supports multicast addressing.

Platform-Specific Notes

Linux

  • Interface names: eth0, wlan0, enp0s3, wlp2s0, lo
  • Predictable network interface names may be used on modern systems

macOS

  • Interface names: en0, en1, lo0, awdl0, utun0
  • en0 is typically the primary Ethernet/Wi-Fi interface

Windows

  • Interface names are more descriptive: Ethernet, Wi-Fi, Loopback Pseudo-Interface 1
  • Names may be localized based on system language

BSD

  • Similar naming to Linux but may have different interface types
  • Interface indices are stable across reboots

Common Use Cases

  • Network monitoring and diagnostics
  • Discovering available network interfaces
  • Checking interface configuration
  • Determining MAC addresses for device identification
  • Validating network connectivity
  • Interface selection for network operations

See Also

Build docs developers (and LLMs) love