Here is a code:

package main

import (
    "fmt"
    "log"
    "net"
)

func main() {

    l, err := net.Listen("tcp", "[::]:9090")
    if err != nil {
        log.Fatal(err)
    }
    defer l.Close()

    go func() {
        for {
            l.Accept()
        }
    }()

    c, err := net.Dial("tcp", "[::]:9090")
    if err != nil {
        log.Fatal(err)
    }
    defer c.Close()

    fmt.Println(l.Addr().String())
    fmt.Println(c.RemoteAddr().String())

    fmt.Println(addrCmp(l.Addr().String(), c.RemoteAddr().String()))
}

func addrCmp(a, b string) bool {
    if ahost, aport, err := net.SplitHostPort(a); err == nil {
        if bhost, bport, err := net.SplitHostPort(b); err == nil {
            if net.ParseIP(ahost).Equal(net.ParseIP(bhost)) {
                return aport == bport
            }
        }
    }
    return false
}

Output is:

[::]:9090
[::1]:9090
false

Is there an easy way to compare two net.Addr or two strings that represents 
network addresses?

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
  • [go-nuts] How to compa... 'Константин Иванов' via golang-nuts

Reply via email to