Readers searching golang get ip address, golang get local ip address, golang get local ip, golang get ip, go get ip address, go ip address, or ip address golang tutorial usually want either all addresses on the machine (linux get local ip / get local ip linux in Go) or a single “primary” LAN address. Golang get hostname pairs with net.LookupHost when DNS-based resolution is acceptable. This guide uses only the standard net and os packages, fixes a common To16 typo, and shows how to skip loopback and optional link-local addresses.
Tested with Go 1.24 on Linux.
List local IPs: net.InterfaceAddrs (golang get local ip)
net.InterfaceAddrs returns address strings as *net.IPNet (or *net.IPAddr). Skip loopback; optionally skip link-local unicast (IsLinkLocalUnicast) if you only want routable LAN/WAN addresses.
package main
import (
"fmt"
"net"
)
func main() {
addrs, err := net.InterfaceAddrs()
if err != nil {
panic(err)
}
for _, a := range addrs {
ipNet, ok := a.(*net.IPNet)
if !ok || ipNet.IP.IsLoopback() {
continue
}
if ipNet.IP.IsLinkLocalUnicast() {
continue
}
fmt.Println(ipNet.IP.String())
}
}Your output depends on the machine; you should see non-loopback IPv4 and/or IPv6 unicast addresses when interfaces are up.
Bug to avoid: use ip.To16() with parentheses if you call it—To16 without () refers to the method value, not the result, and breaks IPv6 checks.
Per-interface listing: net.Interfaces (interface name + IP)
net.Interfaces lists each NIC; call Addrs() on each net.Interface for golang get local ip with names (eth0, docker0, etc.):
package main
import (
"fmt"
"net"
)
func main() {
ifs, err := net.Interfaces()
if err != nil {
panic(err)
}
for _, iface := range ifs {
addrs, err := iface.Addrs()
if err != nil {
continue
}
for _, a := range addrs {
var ip net.IP
switch v := a.(type) {
case *net.IPAddr:
ip = v.IP
case *net.IPNet:
ip = v.IP
default:
continue
}
if ip.IsLoopback() {
continue
}
fmt.Printf("%s\t%s\n", iface.Name, ip.String())
}
}
}Golang get hostname + net.LookupHost
For golang get hostname, use os.Hostname, then net.LookupHost (or LookupIP). This path uses the resolver (/etc/resolv.conf, systemd-resolved, etc.) and may return link-local strings with zone identifiers (%eth0).
package main
import (
"fmt"
"net"
"os"
)
func main() {
h, err := os.Hostname()
if err != nil {
panic(err)
}
fmt.Println("hostname:", h)
addrs, err := net.LookupHost(h)
if err != nil {
panic(err)
}
for _, a := range addrs {
fmt.Println(a)
}
}Prefer interface enumeration when you need the actual NIC list without DNS.
Outbound “default” IP: UDP Dial trick (go get ip address)
To approximate which source IP the kernel would use toward the internet, open a UDP “connection” to a public resolver and read LocalAddr—no packets need to be sent for many stacks:
package main
import (
"fmt"
"net"
)
func main() {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
panic(err)
}
defer conn.Close()
fmt.Println(conn.LocalAddr().(*net.UDPAddr).IP)
}This answers many golang get ip questions where the reader wants one address, not the full interface table. Pick an address appropriate for your environment (corporate DNS, air-gapped nets, IPv6-only, etc.).
Summary
Golang get local ip address flows are usually net.InterfaceAddrs or net.Interfaces on Linux (and other OSes), skipping loopback and optionally link-local. Golang get hostname plus LookupHost is resolver-driven and can differ from NIC state. For a single outbound-style IP, the UDP Dial pattern is common. There is no one perfect “local IP” without defining which network and IPv4 vs IPv6; choose the API that matches that definition. For CLI-style IP discovery outside Go, you may still use tools covered in linux ip command.
References
Frequently Asked Questions
1. How do I golang get local ip address on Linux?
net.InterfaceAddrs() or net.Interfaces() and skip loopback; filter *net.IPNet values and print IP strings—this matches linux get local ip and get local ip linux style discovery in Go.2. What is a simple golang get ip for the default outbound interface?
net.Dial("udp", "8.8.8.8:80") and read LocalAddr—it does not send data but binds a route so IP reflects the chosen source address.3. How does golang get hostname relate to IP lookup?
os.Hostname() then net.LookupHost(hostname) for DNS-based resolution; results depend on resolver and may differ from interface enumeration.4. Why avoid comparing To16 without parentheses?
To16 is a method; you must call To16(); comparing the method value to nil is a compile-time mistake or wrong logic—use proper IP checks like To4() or IsLoopback().
