https://github.com/iiiceoo/iprange
IPv4/IPv6 addresses parser for IP range format.
https://github.com/iiiceoo/iprange
go iprange ipv4 ipv6 networking
Last synced: about 1 year ago
JSON representation
IPv4/IPv6 addresses parser for IP range format.
- Host: GitHub
- URL: https://github.com/iiiceoo/iprange
- Owner: iiiceoo
- License: mit
- Created: 2023-04-13T09:45:22.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-06-28T13:21:54.000Z (almost 2 years ago)
- Last Synced: 2025-04-23T20:16:40.666Z (about 1 year ago)
- Topics: go, iprange, ipv4, ipv6, networking
- Language: Go
- Homepage:
- Size: 74.2 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# iprange
[](https://godoc.org/github.com/iiiceoo/iprange)
[](https://codecov.io/gh/iiiceoo/iprange)
*Package iprange parses IPv4/IPv6 addresses from strings in IP range format.*
## Supported IP range formats
- `172.18.0.1` / `fd00::1`
- `172.18.0.0/24` / `fd00::/64`
- `172.18.0.1-10` / `fd00::1-a`
- `172.18.0.1-172.18.1.10` / `fd00::1-fd00::1:a`
## Example
```go
package main
import (
"fmt"
"log"
"github.com/iiiceoo/iprange"
)
func main() {
// Parse IP ranges.
ranges, err := iprange.Parse(
"172.18.0.1",
"172.18.0.0/24",
"172.18.0.1-10",
"172.18.1.1-172.18.1.3",
)
if err != nil {
log.Fatalf("failed to parse IP ranges: %v\n", err)
}
fmt.Printf("%s IP ranges: %s\n", ranges.Version(), ranges)
// Merge IP ranges.
merged := ranges.Merge()
fmt.Printf("Merged IP ranges: %s\n", merged)
// Interval mathematics of IP ranges.
another, _ := iprange.Parse("172.18.0.0/24")
diff := merged.Diff(another)
fmt.Printf("The difference between two IP ranges: %s\n", diff)
// Convert IP ranges to IP addresses.
fmt.Printf("Iterate through all %d IP addresses:\n", diff.Size())
ipIter := diff.IPIterator()
for {
ip := ipIter.Next()
if ip == nil {
break
}
fmt.Println(ip)
}
}
```
## License
Package iprange is MIT-Licensed.