Created
April 23, 2016 17:37
-
-
Save mantyr/638a03b887d9597ce50b70f6308e8669 to your computer and use it in GitHub Desktop.
Golang - разбить ip диапазон на 4 части
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://play.golang.org/p/JlYJXZnUxl | |
package main | |
import ( | |
"fmt" | |
"net" | |
) | |
func inet_aton(ip string) (ip_int uint32) { | |
ip_byte := net.ParseIP(ip).To4() | |
for i := 0; i < len(ip_byte); i++ { | |
ip_int |= uint32(ip_byte[i]) | |
if i < 3 { | |
ip_int <<= 8 | |
} | |
} | |
return | |
} | |
func inet_ntoa(ip uint32) string { | |
return fmt.Sprintf("%d.%d.%d.%d", byte(ip>>24), byte(ip>>16), byte(ip>>8), | |
byte(ip)) | |
} | |
func main() { | |
ip0 := inet_aton("127.0.0.1") | |
ip1 := inet_aton("127.0.225.224") | |
diff := ip1 - ip0 | |
var parts uint32 = 4 | |
step := diff/parts | |
ipRange := make([]string, parts) | |
for i := uint32(0); i < parts; i++ { | |
ipRange[i] = inet_ntoa(ip0 + step*i) | |
} | |
fmt.Println(ipRange, "127.0.225.224") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment