I am trying to get the IP address and subnet mask by
ifaces, err := net.Interfaces()
for _, iface := range ifaces{
localip, _ = iface.Addrs()
}
However, I am looking forward to get the subnet as something like 255.255.255.0
rather than /24
. How can I get that? If there are no modules that can do this, what is the logic to do this as a program?
Borrowed from some stackoverflow post itself, using logical operators.
mask = (0xFFFFFFFF << (32 - 24)) & 0xFFFFFFFF; //24 is the netmask
var dmask uint64
dmask = 32
localmask := make([]uint64, 0, 4)
for i := 1; i <= 4; i++{
tmp := mask >> (dmask - 8) & 0xFF
localmask = append(localmask, tmp)
dmask -= 8
}
fmt.Println(localmask)