main.go 581 B

12345678910111213141516171819202122232425262728293031
  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. "runtime"
  6. "github.com/vishvananda/netns"
  7. )
  8. func main() {
  9. // Lock the OS Thread so we don't accidentally switch namespaces
  10. runtime.LockOSThread()
  11. defer runtime.UnlockOSThread()
  12. // Save the current network namespace
  13. origns, _ := netns.Get()
  14. defer origns.Close()
  15. // Create a new network namespace
  16. netns.Set
  17. newns, _ := netns.New()
  18. defer newns.Close()
  19. // Do something with the network namespace
  20. ifaces, _ := net.Interfaces()
  21. fmt.Printf("Interfaces: %v\n", ifaces)
  22. // Switch back to the original namespace
  23. netns.Set(origns)
  24. }