https://github.com/seancfoley/ipaddress-go
Go library for handling IP addresses and subnets, both IPv4 and IPv6
https://github.com/seancfoley/ipaddress-go
cidr go golang golang-library golang-module golang-package ip-address ipaddress ipv4 ipv4-address ipv4-network ipv4-subnetting ipv6 ipv6-address ipv6-network ipv6-subnetting mac-address subnet subnets subnetting
Last synced: about 1 year ago
JSON representation
Go library for handling IP addresses and subnets, both IPv4 and IPv6
- Host: GitHub
- URL: https://github.com/seancfoley/ipaddress-go
- Owner: seancfoley
- License: apache-2.0
- Created: 2022-01-03T15:31:08.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-05-19T23:09:07.000Z (about 2 years ago)
- Last Synced: 2024-05-21T00:47:39.036Z (about 2 years ago)
- Topics: cidr, go, golang, golang-library, golang-module, golang-package, ip-address, ipaddress, ipv4, ipv4-address, ipv4-network, ipv4-subnetting, ipv6, ipv6-address, ipv6-network, ipv6-subnetting, mac-address, subnet, subnets, subnetting
- Language: Go
- Homepage: https://seancfoley.github.io/IPAddress/
- Size: 1.82 MB
- Stars: 78
- Watchers: 2
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ipaddress-go
[Go](https://golang.org/) library for handling IP addresses and subnets, both IPv4 and IPv6
IP address and network manipulation, CIDR, address and subnet operations, iterations, containment checks, IP to CIDR block lookup, longest prefix match, subnetting, spanning, merging, ranges, and address tries, with polymorphic code
[View Project Page](https://seancfoley.github.io/IPAddress/)
[View Godoc](https://pkg.go.dev/github.com/seancfoley/ipaddress-go/ipaddr) [](https://pkg.go.dev/github.com/seancfoley/ipaddress-go/ipaddr)
[View Code Examples](https://github.com/seancfoley/ipaddress-go/wiki/Code-Examples)
[View Benchmark Results](https://github.com/seancfoley/ipaddress-go/wiki/Benchmarks)
[View List of Users](https://github.com/seancfoley/ipaddress-go/wiki)
| Version | Notes |
| ------- | ------------- |
| [1.2.1](https://github.com/seancfoley/ipaddress-go/releases/tag/v1.2.1) | Requires Go 1.12 or higher |
| [1.4.1](https://github.com/seancfoley/ipaddress-go/releases/tag/v1.4.1) | Requires Go 1.13 or higher |
| [1.7.1](https://github.com/seancfoley/ipaddress-go/releases/tag/v1.7.1) | Requires Go 1.18 or higher |
In your go.mod file:\
require github.com/seancfoley/ipaddress-go v1.7.1
In your source file:\
import "github.com/seancfoley/ipaddress-go/ipaddr"
Packaged as a [Linux Fedora rpm](https://src.fedoraproject.org/rpms/golang-github-seancfoley-ipaddress) and as a [Linux Debian](https://tracker.debian.org/pkg/golang-github-seancfoley-ipaddress-go), available from [Ubuntu](https://launchpad.net/ubuntu/+source/golang-github-seancfoley-ipaddress-go)
Also available as a [Java](https://www.oracle.com/java/) library from the [IPAddress repository](https://github.com/seancfoley/IPAddress)
## Getting Started
starting with address or subnet strings
```go
import "github.com/seancfoley/ipaddress-go/ipaddr"
ipv6AddrStr := ipaddr.NewIPAddressString("a:b:c:d::a:b/64")
if ipAddr, err := ipv6AddrStr.ToAddress(); err != nil {
// err.Error() has validation error
} else {
// use the address
}
```
...or avoid errors, checking for nil:
```go
str := ipaddr.NewIPAddressString("a:b:c:d:e-f:f:1.2-3.3.4/64")
addr := str.GetAddress()
if addr != nil {
// use address
}
```
starting with host name strings
```go
hostStr := "[::1]"
host := ipaddr.NewHostName(hostStr)
err := host.Validate()
if err == nil {
if host.IsAddress() {
fmt.Println("address: " + host.AsAddress().String())
} else {
fmt.Println("host name: " + host.String())
}
// use host
} else {
fmt.Println(err.Error())
}
```