Golang : Get the IPv4 and IPv6 addresses for a specific network interface
Problem:
You need to find the IP address of a specific network interface on your machine for developing your application. Perhaps your machine has couple of network interfaces and your application needs to use a specific interface for security reason.
How to find the IP address either version 4 or 6?
Solution:
Use net
package Interfaces()
function. Below is an example of how to use the Interfaces()
to find out the available interfaces and then specify a network interface by name to list out the IP addresses.
NOTE : This example will enumerate all the available network interfaces. If you need to specify a particular interface on your machine, first find out the interface name and then plug in the name in net.InterfaceByName("name")
.
Here you go!
package main
import (
"fmt"
"net"
)
func main() {
// get available network interfaces for
// this machine
interfaces, err := net.Interfaces()
if err != nil {
fmt.Print(err)
return
}
for _, i := range interfaces {
fmt.Printf("Name : %v \n", i.Name)
byNameInterface, err := net.InterfaceByName(i.Name)
if err != nil {
fmt.Println(err)
}
//fmt.Println("Interface by Name : ", byNameInterface)
addresses, err := byNameInterface.Addrs()
for k, v := range addresses {
fmt.Printf("Interface Address #%v : %v\n", k, v.String())
}
fmt.Println("------------------------------------")
}
}
Sample output on my machine. Your machine result will be different.
Name : lo0
Interface Address #0 : ::1/128
Interface Address #1 : 127.0.0.1/8
Interface Address #2 : fe80::1/64
------------------------------------
Name : gif0
------------------------------------
Name : stf0
------------------------------------
Name : en3
Interface Address #0 : fe80::1aa6:f7ff:fe16:e80b/64
Interface Address #1 : 192.168.1.64/24
------------------------------------
If the network interface has no IP address, usually it means the status is DOWN.
References:
See also : Golang : How to detect a server/machine network interface capabilities?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+8.9k Golang : How to get garbage collection data?
+13.6k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+4.7k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+6.7k CloudFlare : Another way to get visitor's real IP address
+9k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
+6.1k Golang : Calculate diameter, circumference, area, sphere surface and volume
+26.8k Golang : dial tcp: too many colons in address
+5.9k Golang : Scan forex opportunities by Bollinger bands
+11.2k Golang : Display a text file line by line with line number example
+4.9k Python : Convert(cast) string to bytes example
+9.3k Golang : Turn string or text file into slice example
+36.7k Upload multiple files with Go