FAQ
Hi,

In many software projects I have to get the address family of a net.IP
object. I always duplicate the following code:

func isIPv4(ip net.IP) bool {
     return len(ip) == net.IPv4len || (len(ip) > 11 && isZeros(ip[0:10]) &&
ip[10] == 0xff && ip[11] == 0xff)
}

func isZeros(ip net.IP) bool {
     for _, b := range ip {
         if b != 0 {
             return false
         }
     }
     return true
}

Instead it would be very very useful to have a Family(), IsIPv4 or IsIPv6
function on net.IP.
What is your opinion on that?


--
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/d/optout.

Search Discussions

  • Paul Borman at Jun 16, 2016 at 3:18 pm
    Do you mean like To4 and To16 that are defined on net.IP?

    switch {
    case ip.To4() != nil:
         // is an IPv4 address
    case ip.To16() != nil:
         // is an IPv6 address
    default:
         // is something else
    }
    On Wed, Jun 15, 2016 at 11:29 PM, wrote:

    Hi,

    In many software projects I have to get the address family of a net.IP
    object. I always duplicate the following code:

    func isIPv4(ip net.IP) bool {
    return len(ip) == net.IPv4len || (len(ip) > 11 && isZeros(ip[0:10]) &&
    ip[10] == 0xff && ip[11] == 0xff)
    }

    func isZeros(ip net.IP) bool {
    for _, b := range ip {
    if b != 0 {
    return false
    }
    }
    return true
    }

    Instead it would be very very useful to have a Family(), IsIPv4 or IsIPv6
    function on net.IP.
    What is your opinion on that?


    --
    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/d/optout.
    --
    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/d/optout.
  • Google at Jun 17, 2016 at 1:00 am
    Ok, that's sufficient for me. It's only dangerous that if you switch the
    cases (To16() first) it does not work any more.

    Am Donnerstag, 16. Juni 2016 17:18:37 UTC+2 schrieb Paul Borman:
    Do you mean like To4 and To16 that are defined on net.IP?

    switch {
    case ip.To4() != nil:
    // is an IPv4 address
    case ip.To16() != nil:
    // is an IPv6 address
    default:
    // is something else
    }
    --
    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/d/optout.
  • Paul Borman at Jun 17, 2016 at 2:27 pm
    Yes, To16 gives you a 16 byte version of an IP address, which can be IPv4
    or IPv6. Normally it is sufficient to just call To4 to determine if it is
    an IPv4 address or not and assume if it is not it must be an IPv6 address:

    func IsIPv4(ip net.IP) bool { return ip.To4() != nil }
    func IsIPv6(ip net.IP) bool { return ip.To4() == nil } // simple version
    func IsIPv6(ip net.IP) bool { return ip.To4() == nil && ip.To16() != nil }
    // more robust version
    On Thu, Jun 16, 2016 at 6:00 PM, wrote:

    Ok, that's sufficient for me. It's only dangerous that if you switch the
    cases (To16() first) it does not work any more.

    Am Donnerstag, 16. Juni 2016 17:18:37 UTC+2 schrieb Paul Borman:
    Do you mean like To4 and To16 that are defined on net.IP?

    switch {
    case ip.To4() != nil:
    // is an IPv4 address
    case ip.To16() != nil:
    // is an IPv6 address
    default:
    // is something else
    }
    --
    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/d/optout.
    --
    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/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 16, '16 at 3:02p
activeJun 17, '16 at 2:27p
posts4
users2
websitegolang.org

2 users in discussion

Paul Borman: 2 posts Google: 2 posts

People

Translate

site design / logo © 2021 Grokbase