How do I get the local IP address in Go?

golang get public ip address
golang get ip address from http request
golang ip to int
golang get all ip addresses
go get local address
go get current ip address
golang find local ip address
golang get interface address

I want to get the computer's IP address. I used the code below, but it returns

I want to get the IP address, such as, instead of the loopback address.

name, err := os.Hostname()
if err != nil {
     fmt.Printf("Oops: %v\n", err)

addrs, err := net.LookupHost(name)
if err != nil {
    fmt.Printf("Oops: %v\n", err)

for _, a := range addrs {

You need to loop through all network interfaces

ifaces, err := net.Interfaces()
// handle err
for _, i := range ifaces {
    addrs, err := i.Addrs()
    // handle err
    for _, addr := range addrs {
        var ip net.IP
        switch v := addr.(type) {
        case *net.IPNet:
                ip = v.IP
        case *net.IPAddr:
                ip = v.IP
        // process IP address

Play (taken from util/helper.go)

Golang Examples (Go Examples), Get local IP addresses Use net.InterfaceAddrs(). addrs, err := net.InterfaceAddrs() if err != nil { panic(err) } for i, addr := range addrs { fmt. The IP address, subnet, and router (gateway) will all be there under the DHCP tab. If you need the IP address of other devices on your network, go into the router.

Here is a better solution to retrieve the preferred outbound ip address when there are multiple ip interfaces exist on the machine.

import (

// Get preferred outbound ip of this machine
func GetOutboundIP() net.IP {
    conn, err := net.Dial("udp", "")
    if err != nil {
    defer conn.Close()

    localAddr := conn.LocalAddr().(*net.UDPAddr)

    return localAddr.IP

Get My IP Golang · GitHub, myip.go. package main. /*. URL:​myip.go. URL: */. import (. To know your Local IP Address of your computer, follow the below given steps. Click on the Start menu of your Windows OS, Operating System, and type cmd . When you see the cmd applications in Start menu panel, click it or just press enter.

To ensure that you get a non-loopback address, simply check that an IP is not a loopback when you are iterating.

// GetLocalIP returns the non loopback local IP of the host
func GetLocalIP() string {
    addrs, err := net.InterfaceAddrs()
    if err != nil {
        return ""
    for _, address := range addrs {
        // check the address type and if it is not a loopback the display it
        if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
            if ipnet.IP.To4() != nil {
                return ipnet.IP.String()
    return ""

src/net/ip.go, 4 5 // IP address manipulations 6 // 7 // IPv4 addresses are 4 bytes; IPv6 166 // It returns true even if ip is in IPv4 private address space or 167 // local IPv6  Under Properties, look for your IP address listed next to IPv4 address. For Ethernet connection. On the taskbar, select the Ethernet network icon > the Ethernet network connection. Under Ethernet, select the Ethernet network connection. Under Properties, look for your IP address listed next to IPv4 address.

net.LookupHost() on your os.Hostname() is probably always going to give you, because that's what's in your /etc/hosts or equivalent.

I think what you want to use is net.InterfaceAddrs():

func InterfaceAddrs() ([]Addr, error)

InterfaceAddrs returns a list of the system's network interface addresses.

Get ipaddress from hostname, the local resolver. It returns an array of that host's IPv4 and IPv6 addresses. Here is a go lang example that shows how get an ipaddress for a given host name. An IP address (short for Internet Protocol address) is used to identify computers on the Internet. It works like a return address would on a piece of mail. When your computer or device sends a request, like a search on Google, it tags the request with your IP address. That way Google knows where to send the response.

This worked for me:

host, _ := os.Hostname()
addrs, _ := net.LookupIP(host)
for _, addr := range addrs {
    if ipv4 := addr.To4(); ipv4 != nil {
        fmt.Println("IPv4: ", ipv4)

Unlike the poster's example, it returns only non-loopback addresses, e.g. 10.120.X.X.

Find the local IP address, The function below returns the local IP address if you give it a server. that it should LookupHost: Step 2. The command prompt should open; now in the open line, you’ll need to type in ipconfig and press Enter. You’ll see your IP address listed right above the subnet mask. Usually, it will say IPv4 Address and follow the prefix 192.168.1.# or 192.168.0.# for home networks as shown in the screenshot above.

Golang - How to Get Local Mac and IP Address?, Get the local machine IP address //​get-local-ip-and-mac-address //---------------------- addrs,  How to Go to an IP Address Instead of a URL. thing the first time in order for the web browser to recognize that I wanted an IP address and not a search

Go combat--golang Get public IP, view intranet IP, detect IP type , Go Language Learning Net package (the path to go) brief aftertaste net Parseip parses S as an IP address, returning the result. get local IP There are a few ways you can find your IP address. Let’s start with the easiest method that allows you to use the UI and not the command line. Go to Settings > Network & Internet. Then select either Wi-Fi or Ethernet (depending on how the device connects). In this example, I’m connected to Ethernet.

How to Find Your Local and External IP Address, If you're having trouble finding out its IP address that way, you can always go the reverse approach: your router. Somewhere in your router's  The easiest way to find your public IP address is by asking a website, since that website sees your public IP address and can tell it to you.

  • Thanks, added it to the answer
  • util/helper.go is probably this… . What about IPv6? This only seems to work for IPv4 Addresses.
  • The call to net.Interfaces() returns no error and 0 interfaces on my Linux host (Manjaro, current) :(
  • @Sebastian I fat fingered voting on my phone and accidentally gave you a downvote, sorry. If you make a trivial edit I'll fix that.…
  • @Sebastian When do you expect the IPAddr case instead of the IPNet case?
  • I was searching for the same and this is only one that worked fine for me :) , By the way i wanted to know why you are doing udp connection to Google Dns and whats the value whole of "conn.LocalAddr().String()" ?
  • Actually it does not establish any connection and the destination does not need to be existed at all :) So, what the code does actually, is to get the local up address if it would connect to that target, you can change to any other IP address you want. conn.LocalAddr().String() is the local ip and port.
  • @darkwing it is using the udp protocol, unlike TCP, it does not have handshake nor a connection. hence, the target does not need be there and you will receive the outbound IP.
  • @darkwing: See it as a "logical" connection, not a "physical" one.
  • @SanketSudake any port number is ok. there is no connection established
  • This is a good solution as long as there is only one non-loopback interface available.
  • This has its own wrinkles. The addresses are provided as strings. Given an IP address (specific Go type, not the generic term) there is no direct comparison. It is either necessary to convert the IP to a string and compare or parse the strings to get the IP to compare, neither of which looks trivial to this neophyte Go programmer. I'm looking for library procedures to convert the string to an IP but have not yet found one.
  • What is the IP address Go type you refer to?
  • type IP []byte And the parser I seek is just below that on the page: func ParseCIDR(s string) (IP, *IPNet, error) (Cannot seem to format those lines as code.)
  • but if i use if loop it will be local inside that loop... i want to use the ipv4 value outside this loop