Skip to content

Instantly share code, notes, and snippets.

@iCoolchar
Created February 26, 2017 13:09
Show Gist options
  • Save iCoolchar/1e76c9b897f25e9378f1963ceb6ea550 to your computer and use it in GitHub Desktop.
Save iCoolchar/1e76c9b897f25e9378f1963ceb6ea550 to your computer and use it in GitHub Desktop.
Golang ip to int
func IP4toInt(IPv4Addr string) int64 {
IPv4Int := big.NewInt(0)
IPv4Int.SetBytes(net.ParseIP(IPv4Addr).To4())
return IPv4Int.Int64()
}
@tangxinfa
Copy link

binary.LittleEndian.Uint32(net.ParseIP(IPv4Addr).To4())
Convert to a network order(little endian) 4 byte integer, that is broadly used in C network programming

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment