A golang library for discovering the address of a LAN gateway.
Go
249
113 commits
updated Sep 4, 2026
A simple library for discovering the IP address of the default gateway.
Supports both IPv4 and IPv6.
Maintenance status: ✅ Maintained (stable). This library is feature-complete — I accept bug fixes and security patches, but am not adding new features. See SECURITY.md for how to report vulnerabilities.
package main
import (
"fmt"
"github.com/jackpal/gateway"
)
func main() {
gw, err := gateway.DiscoverGateway()
if err != nil {
fmt.Println(err)
} else {
fmt.Println("IPv4 Gateway:", gw.String())
}
}
package main
import (
"fmt"
"github.com/jackpal/gateway"
)
func main() {
gw, err := gateway.DiscoverGatewayIPv6()
if err != nil {
fmt.Println(err)
} else {
fmt.Println("IPv6 Gateway:", gw.String())
}
}
| Platform | IPv4 | IPv6 |
|---|---|---|
| Darwin (macOS) | ✅ | ✅ |
| Dragonfly | ✅ | ✅ |
| FreeBSD | ✅ | ✅ |
| Linux | ✅ | ✅ |
| NetBSD | ✅ | ✅ |
| OpenBSD | ✅ | ✅ |
| Solaris / illumos | ✅ | ✅ |
| Windows | ✅ | ✅ |
Other platforms return an error.
Pull requests for other OSs happily considered!
DiscoverGatewayIPv6(), DiscoverGatewaysIPv6(), DiscoverInterfaceIPv6().
DiscoverGateways() and DiscoverGatewaysIPv6()._test.go suffix).func DiscoverGateways() ([]net.IP, error). This func returns an array of gateways that contains at least one gateway. Otherwise returns an error.Update x/net dependency. This was done to squelch a github security alert caused by depending upon an old version of x/net.
Update dependencies to latest versions. This was done to squelch a github security alert caused by depending upon an old version of x/net. This is the first time I've updated module versions, the tests pass, so hopefully everything's good.
netstat on darwin systems if route fails.Overall
Maintained
Code review
Go module
Latest release
Releases this year
A golang library for discovering the address of a LAN gateway.
Go
249
113 commits
updated Sep 4, 2026
A simple library for discovering the IP address of the default gateway.
Supports both IPv4 and IPv6.
Maintenance status: ✅ Maintained (stable). This library is feature-complete — I accept bug fixes and security patches, but am not adding new features. See SECURITY.md for how to report vulnerabilities.
package main
import (
"fmt"
"github.com/jackpal/gateway"
)
func main() {
gw, err := gateway.DiscoverGateway()
if err != nil {
fmt.Println(err)
} else {
fmt.Println("IPv4 Gateway:", gw.String())
}
}
package main
import (
"fmt"
"github.com/jackpal/gateway"
)
func main() {
gw, err := gateway.DiscoverGatewayIPv6()
if err != nil {
fmt.Println(err)
} else {
fmt.Println("IPv6 Gateway:", gw.String())
}
}
| Platform | IPv4 | IPv6 |
|---|---|---|
| Darwin (macOS) | ✅ | ✅ |
| Dragonfly | ✅ | ✅ |
| FreeBSD | ✅ | ✅ |
| Linux | ✅ | ✅ |
| NetBSD | ✅ | ✅ |
| OpenBSD | ✅ | ✅ |
| Solaris / illumos | ✅ | ✅ |
| Windows | ✅ | ✅ |
Other platforms return an error.
Pull requests for other OSs happily considered!
DiscoverGatewayIPv6(), DiscoverGatewaysIPv6(), DiscoverInterfaceIPv6().
DiscoverGateways() and DiscoverGatewaysIPv6()._test.go suffix).func DiscoverGateways() ([]net.IP, error). This func returns an array of gateways that contains at least one gateway. Otherwise returns an error.Update x/net dependency. This was done to squelch a github security alert caused by depending upon an old version of x/net.
Update dependencies to latest versions. This was done to squelch a github security alert caused by depending upon an old version of x/net. This is the first time I've updated module versions, the tests pass, so hopefully everything's good.
netstat on darwin systems if route fails.Overall
Maintained
Code review
Go module
Latest release
Releases this year