Skip to content

xgfone/go-linux-namespace

Repository files navigation

Linux Namespace Build Status GoDoc License

The operation about linux namespace. Support Go1.7+.

Installation

$ go get -u github.com/xgfone/go-linux-namespace

Example

package main

import (
    "fmt"

    "github.com/xgfone/go-linux-namespace"
)

func main() {
    ns := NewNameSpace("name")

    // Ensure that the namespace exists.
    if exist, err := ns.IsExist(); err != nil {
        fmt.Println(err)
        return
    } else if !exist {
        if err := ns.Create(); err != nil {
            fmt.Println(err)
            return
        }
    }

    // Execute the command in the namespace.
    if output, err := ns.Exec("ip", "a"); err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(output)
    }

    // Delete the namespace.
    if err := ns.Delete(); err != nil {
        fmt.Println(err)
        return
    }
}