Golang net.Interfaces() function examples

package net

Golang net.Interfaces() function usage examples

Example 1:

 package main

 import (
 "fmt"
 "net"
 )

 func main() {

 interfaces, err := net.Interfaces()

 if err != nil {
 fmt.Print(err)
 return
 }

 for _, i := range interfaces {

 fmt.Printf("Name : %v \n", i.Name)

 // see http://golang.org/pkg/net/#Flags
 fmt.Println("Interface type and supports : ", i.Flags.String())
 }

 }

Example 2:

 ifaces, err := net.Interfaces()
 if err != nil {
 fmt.Print(err)
 return
 }
 for _, i := range ifaces {
 addrs, err := i.Addrs()
 if err != nil {
 fmt.Print(err)
 continue
 }
 for _, a := range addrs {
 switch v := a.(type) {
 case *net.IPAddr:
 fmt.Printf("%v : %s (%s)\n", i.Name, v, v.IP.DefaultMask())
 }

 }
 }

References :

http://golang.org/pkg/net/#Interfaces

Advertisement