Golang net.InterfaceAddrs() function example
package net
Golang net.InterfaceAddrs() function usage example
package main
import (
"fmt"
"net"
"os"
)
func main() {
addrs, err := net.InterfaceAddrs()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
fmt.Println(ipnet.IP.String())
}
}
}
}
References :
http://golang.org/pkg/net/#InterfaceAddrs
https://www.socketloop.com/tutorials/golang-how-do-I-get-the-local-ip-non-loopback-address
Advertisement
Something interesting
Tutorials
+25k Golang : Create PDF file from HTML file
+7.2k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+16.9k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+12.3k Golang : Validate email address
+8.4k Golang : How to check if input string is a word?
+6.4k PHP : Proper way to get UTF-8 character or string length
+5.8k Javascript : How to replace HTML inside <div>?
+19.2k Golang : Check if directory exist and create if does not exist
+22.9k Golang : Calculate time different
+31.5k Golang : bufio.NewReader.ReadLine to read file line by line
+16.9k Golang : How to generate QR codes?
+7.3k Golang : Not able to grep log.Println() output