Golang : Get local IP and MAC address
This is a short program on how to get the local IP address, search for the correct network interface hardware name and get the MAC address. Remember, this example is not able to get MAC address of remote/client machine. ;-)
Here you go!
getlocalIPandMACaddr.go
package main
import (
"fmt"
"net"
"os"
"strings"
)
func main() {
//----------------------
// Get the local machine IP address
// https://www.socketloop.com/tutorials/golang-how-do-I-get-the-local-ip-non-loopback-address
//----------------------
addrs, err := net.InterfaceAddrs()
if err != nil {
fmt.Println(err)
}
var currentIP, currentNetworkHardwareName string
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
// = GET LOCAL IP ADDRESS
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
fmt.Println("Current IP address : ", ipnet.IP.String())
currentIP = ipnet.IP.String()
}
}
}
fmt.Println("------------------------------")
fmt.Println("We want the interface name that has the current IP address")
fmt.Println("MUST NOT be binded to 127.0.0.1 ")
fmt.Println("------------------------------")
// get all the system's or local machine's network interfaces
interfaces, _ := net.Interfaces()
for _, interf := range interfaces {
if addrs, err := interf.Addrs(); err == nil {
for index, addr := range addrs {
fmt.Println("[", index, "]", interf.Name, ">", addr)
// only interested in the name with current IP address
if strings.Contains(addr.String(), currentIP) {
fmt.Println("Use name : ", interf.Name)
currentNetworkHardwareName = interf.Name
}
}
}
}
fmt.Println("------------------------------")
// extract the hardware information base on the interface name
// capture above
netInterface, err := net.InterfaceByName(currentNetworkHardwareName)
if err != nil {
fmt.Println(err)
}
name := netInterface.Name
macAddress := netInterface.HardwareAddr
fmt.Println("Hardware name : ", name)
fmt.Println("MAC address : ", macAddress)
// verify if the MAC address can be parsed properly
hwAddr, err := net.ParseMAC(macAddress.String())
if err != nil {
fmt.Println("No able to parse MAC address : ", err)
os.Exit(-1)
}
fmt.Printf("Physical hardware address : %s \n", hwAddr.String())
}
Sample output:
Current IP address : 192.168.1.65
------------------------------
We want the interface name that has the current IP address
MUST NOT be binded to 127.0.0.1
------------------------------
[ 0 ] lo0 > ::1/128
[ 1 ] lo0 > 127.0.0.1/8
[ 2 ] lo0 > fe80::1/64
[ 0 ] en3 > fe80::1aa6:f7ff:fe16:e80b/64
[ 1 ] en3 > 192.168.1.65/24
Use name : en3
------------------------------
Hardware name : en3
MAC address : 18:a6:f7:16:e8:0b
Physical hardware address : 18:a6:f7:16:e8:0b
References:
https://www.socketloop.com/tutorials/golang-how-do-I-get-the-local-ip-non-loopback-address
See also : Golang : Find network of an IP address
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.7k Golang : Another camera capture GUI application with GTK and OpenCV
+30.4k Golang : How to verify uploaded file is image or allowed file types
+27.5k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+10k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+6k Linux/MacOSX : Search for files by filename and extension with find command
+12k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+14k Golang : convert rune to unicode hexadecimal value and back to rune character
+17.9k Golang : Iterate linked list example
+11.5k Use systeminfo to find out installed Windows Hotfix(s) or updates
+12k Golang : How to parse plain email text and process email header?
+11.4k Golang : Post data with url.Values{}