I Spy Code - GO

Get list of system IP addresses

Question:

Ho do you get the list of IP addresses associated with your system?

Answer:

InterfaceAddrs returns a list of the system's network interface addresses.

Here is a go lang example that shows how to get a list of all the ip addresses associated with your system.

Source: (example.go)

package main
 
import (
   "fmt"
   "net"
)
 
func main() {
   foo, err := net.InterfaceAddrs()
 
   if err == nil {
      for _, v := range foo {
         fmt.Println(v)
 
      }
   }
}
 

Output:

$ go run example.go
127.0.0.1/8
192.168.2.8/24
::1/128
fe80::dcb3:295c:755b:91bb/64

References:

https://golang.org/pkg/net/#InterfaceAddrs