Skip to main content
The net package provides comprehensive network monitoring capabilities including I/O statistics, connection tracking, interface information, and protocol counters.

Key Features

I/O Counters

Track bytes, packets, errors, and drops for network interfaces

Connections

Monitor active network connections and their states

Interfaces

Get detailed information about network interfaces

Protocol Stats

Access protocol-specific network statistics

Main Functions

IOCounters

Retrieves network I/O statistics for all network interfaces.
func IOCounters(pernic bool) ([]IOCountersStat, error)
func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error)
Parameters:
  • pernic - If true, returns statistics per interface; if false, returns aggregated statistics

Connections

Retrieves a list of active network connections.
func Connections(kind string) ([]ConnectionStat, error)
func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error)
Parameters:
  • kind - Connection type filter (e.g., “tcp”, “udp”, “inet”, “inet4”, “inet6”, “unix”, “all”)

Interfaces

Retrieves information about all network interfaces.
func Interfaces() (InterfaceStatList, error)
func InterfacesWithContext(ctx context.Context) (InterfaceStatList, error)

ProtoCounters

Retrieves network protocol statistics.
func ProtoCounters(protocols []string) ([]ProtoCountersStat, error)
func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error)
Parameters:
  • protocols - List of protocols to query (e.g., “ip”, “icmp”, “tcp”, “udp”). Empty list returns all protocols
Note: Not implemented on FreeBSD, Windows, OpenBSD, and Darwin

Additional Functions

Connection Filtering

// Get connections for a specific process
func ConnectionsPid(kind string, pid int32) ([]ConnectionStat, error)

// Get up to max connections per process
func ConnectionsMax(kind string, maxConn int) ([]ConnectionStat, error)
func ConnectionsPidMax(kind string, pid int32, maxConn int) ([]ConnectionStat, error)

// Get connections without UID information (performance optimization)
func ConnectionsWithoutUids(kind string) ([]ConnectionStat, error)
func ConnectionsPidWithoutUids(kind string, pid int32) ([]ConnectionStat, error)

Conntrack Statistics

// Get iptables conntrack statistics
func FilterCounters() ([]FilterStat, error)

// Get detailed conntrack table information
func ConntrackStats(percpu bool) ([]ConntrackStat, error)

Usage Example

package main

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

func main() {
    // Get I/O counters for all interfaces
    counters, err := net.IOCounters(true)
    if err != nil {
        panic(err)
    }
    
    for _, counter := range counters {
        fmt.Printf("%s: sent=%d, recv=%d\n", 
            counter.Name, counter.BytesSent, counter.BytesRecv)
    }
    
    // Get all TCP connections
    conns, err := net.Connections("tcp")
    if err != nil {
        panic(err)
    }
    
    fmt.Printf("Active TCP connections: %d\n", len(conns))
    
    // Get interface information
    interfaces, err := net.Interfaces()
    if err != nil {
        panic(err)
    }
    
    for _, iface := range interfaces {
        fmt.Printf("Interface: %s (MTU: %d)\n", iface.Name, iface.MTU)
    }
}

Platform Support

Most functions are supported across all platforms, with these exceptions:
  • ProtoCounters: Not available on FreeBSD, Windows, OpenBSD, Darwin
  • FilterCounters and ConntrackStats: Linux-specific features
  • ConnectionsWithoutUids variants: Implementation-specific, may vary by platform

See Also

Build docs developers (and LLMs) love