...

Text file src/github.com/vishvananda/netns/README.md

Documentation: github.com/vishvananda/netns

     1# netns - network namespaces in go #
     2
     3The netns package provides an ultra-simple interface for handling
     4network namespaces in go. Changing namespaces requires elevated
     5privileges, so in most cases this code needs to be run as root.
     6
     7## Local Build and Test ##
     8
     9You can use go get command:
    10
    11    go get github.com/vishvananda/netns
    12
    13Testing (requires root):
    14
    15    sudo -E go test github.com/vishvananda/netns
    16
    17## Example ##
    18
    19```go
    20package main
    21
    22import (
    23    "fmt"
    24    "net"
    25    "runtime"
    26
    27    "github.com/vishvananda/netns"
    28)
    29
    30func main() {
    31    // Lock the OS Thread so we don't accidentally switch namespaces
    32    runtime.LockOSThread()
    33    defer runtime.UnlockOSThread()
    34
    35    // Save the current network namespace
    36    origns, _ := netns.Get()
    37    defer origns.Close()
    38
    39    // Create a new network namespace
    40    newns, _ := netns.New()
    41    defer newns.Close()
    42
    43    // Do something with the network namespace
    44    ifaces, _ := net.Interfaces()
    45    fmt.Printf("Interfaces: %v\n", ifaces)
    46
    47    // Switch back to the original namespace
    48    netns.Set(origns)
    49}
    50
    51```

View as plain text