FAQ
Any ideas on how to list all of the local IP Address and Hardware Addresses?

I understand the following, it provides Hardware Address...but the
Interface struct does not provide IP Address.

interfaces, _ := net.Interfaces()
for _, inter := range interfaces {
         fmt.Println(inter.Name, inter.HardwareAddr)
}

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Martin Schnabel at Dec 15, 2013 at 9:50 am
    package main

    import "fmt"
    import "net"

    func main() {
      interfaces, _ := net.Interfaces()
      for _, inter := range interfaces {
       fmt.Println(inter.Name, inter.HardwareAddr)
       fmt.Println(inter.Addrs())
      }

    }

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Péter Szilágyi at Dec 15, 2013 at 9:51 am
    Hi,

       This should do it:

    package main

    import (
    "fmt"
    "net"
    )

    func main() {
    interfaces, _ := net.Interfaces()
    for _, inter := range interfaces {
    fmt.Println(inter.Name, inter.HardwareAddr)
    if addrs, err := inter.Addrs(); err == nil {
    for _, addr := range addrs {
    fmt.Println(inter.Name, "->", addr)
    }
    }
    }
    }

    Output:
    lo
    lo -> 127.0.0.1/8
    lo -> ::1/128
    eth0 00:1e:ec:8b:f2:63
    wlan0 00:16:ea:b5:e8:e0
    wlan0 -> 192.168.0.196/24
    wlan0 -> fe80::216:eaff:feb5:e8e0/64

    Cheers,
       Peter
    On Sun, Dec 15, 2013 at 11:30 AM, Curtis Paul wrote:

    Any ideas on how to list all of the local IP Address and Hardware
    Addresses?

    I understand the following, it provides Hardware Address...but the
    Interface struct does not provide IP Address.

    interfaces, _ := net.Interfaces()
    for _, inter := range interfaces {
    fmt.Println(inter.Name, inter.HardwareAddr)
    }

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Kevin Gillette at Dec 15, 2013 at 9:51 am
    Each inter has an Addrs() method (see
    http://golang.org/pkg/net/#Interface.Addrs).
    On Sunday, December 15, 2013 2:30:02 AM UTC-7, Curtis Paul wrote:

    Any ideas on how to list all of the local IP Address and Hardware
    Addresses?

    I understand the following, it provides Hardware Address...but the
    Interface struct does not provide IP Address.

    interfaces, _ := net.Interfaces()
    for _, inter := range interfaces {
    fmt.Println(inter.Name, inter.HardwareAddr)
    }
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Michael Gehring at Dec 15, 2013 at 9:51 am

    On Sun, Dec 15, 2013 at 01:30:02AM -0800, Curtis Paul wrote:
    Any ideas on how to list all of the local IP Address and Hardware Addresses?

    I understand the following, it provides Hardware Address...but the
    Interface struct does not provide IP Address.
    http://golang.org/pkg/net/#Interface.Addrs

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Curtis Paul at Dec 15, 2013 at 6:03 pm
    Péter's solution put it into perspective for me...
    On Sunday, December 15, 2013 2:30:02 AM UTC-7, Curtis Paul wrote:

    Any ideas on how to list all of the local IP Address and Hardware
    Addresses?

    I understand the following, it provides Hardware Address...but the
    Interface struct does not provide IP Address.

    interfaces, _ := net.Interfaces()
    for _, inter := range interfaces {
    fmt.Println(inter.Name, inter.HardwareAddr)
    }
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 15, '13 at 9:30a
activeDec 15, '13 at 6:03p
posts6
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase