https://github.com/pchchv/goip
Go library for handling IP addresses and subnets
https://github.com/pchchv/goip
go go-library go-module go-package golang golang-library golang-module golang-package ip ipv4 ipv4-network ipv6 ipv6-network subnetting
Last synced: 12 months ago
JSON representation
Go library for handling IP addresses and subnets
- Host: GitHub
- URL: https://github.com/pchchv/goip
- Owner: pchchv
- License: apache-2.0
- Created: 2023-06-07T08:07:42.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-12-24T10:04:44.000Z (over 2 years ago)
- Last Synced: 2025-03-03T08:23:57.442Z (over 1 year ago)
- Topics: go, go-library, go-module, go-package, golang, golang-library, golang-module, golang-package, ip, ipv4, ipv4-network, ipv6, ipv6-network, subnetting
- Language: Go
- Homepage:
- Size: 1.89 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# goip [](https://pkg.go.dev/github.com/pchchv/goip)
Go package for handling IP addresses and subnets. IPv4 and IPv6.
Working with IP addresses and networks, CIDR, address and subnet operations, iterations, content checks, IP to CIDR block lookup, longest prefix match, creating subnets, spanning, merging, ranges and address tries, with polymorphic code
## Usage
starting with address or subnet strings
```go
import "github.com/pchchv/goip"
ipv6AddrStr := goip.NewIPAddressString("a:b:c:d::a:b/64")
if ipAddr, err := ipv6AddrStr.ToAddress(); err != nil {
// error validation
} else {
// use the address
}
```
...or checking for nil:
```go
str := goip.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:
```go
hostStr := "[::1]"
host := goip.NewHostName(hostStr)
if err := host.Validate(); err != nil {
panic(err)
}
// use host
if host.IsAddress() {
fmt.Println("address: " + host.AsAddress().String())
} else {
fmt.Println("host name: " + host.String())
}
```