How to get the port from a net.Addr

909 views
Skip to first unread message

Rory McGuire

unread,
Oct 11, 2012, 7:22:13 AM10/11/12
to golan...@googlegroups.com
Hi,

I'm using go.crypto.ssh.DialTCP(....) (net.Conn, error) to get a tunnel to another server. :D problem is I need the local port number.

Why can I not get the Port from a TCPAddr that is currently a Addr if the error I'm getting is:
impossible type assertion: conn (type net.Conn) cannot have dynamic type net.TCPConn (missing Close method)

which is from this:
 conn.(net.TCPConn).LocalAddr().(net.TCPAddr).Port

This error is nonsense to me because both net.Conn and net.TCPConn require a Close method.

Dan Kortschak

unread,
Oct 11, 2012, 7:31:21 AM10/11/12
to Rory McGuire, golan...@googlegroups.com
*net.TCPConn has a Close method.

On Thu, 2012-10-11 at 04:22 -0700, Rory McGuire wrote:
> Hi,
>
> I'm using go.crypto.ssh.DialTCP(....) (net.Conn, error) to get a tunnel to
> another server. :D problem is I need the local port number.
>
> Why can I not get the Port from a TCPAddr that is currently a Addr if the
> error I'm getting is:
> *impossible type assertion: conn (type net.Conn) cannot have dynamic type
> net.TCPConn (missing Close method)*
> *
> *
> which is from this:
> * conn.(net.TCPConn).LocalAddr().(net.TCPAddr).Port*

Jan Mercl

unread,
Oct 11, 2012, 7:32:05 AM10/11/12
to Rory McGuire, golan...@googlegroups.com
On Thu, Oct 11, 2012 at 1:22 PM, Rory McGuire <rjmc...@gmail.com> wrote:
> This error is nonsense to me because both net.Conn and net.TCPConn require a
> Close method.

What follows I didn't verified:

It's not a nonsense, TCPConn.Close requires a pointer receiver. Try

conn.(*net.TCPConn)

instead.

HTH

-j

Rory McGuire

unread,
Oct 11, 2012, 9:48:11 AM10/11/12
to golan...@googlegroups.com, Rory McGuire
On Thursday, 11 October 2012 13:32:12 UTC+2, Jan Mercl wrote:

It's not a nonsense, TCPConn.Close requires a pointer receiver. Try

Doh! Thanks guys. 

Rory McGuire

unread,
Oct 11, 2012, 9:53:43 AM10/11/12
to golan...@googlegroups.com, Rory McGuire
Just for completeness. To get the port from go.crypt.ssh.ClientConn.DialTCP use:
conn.LocalAddr().(*net.TCPAddr).Port
Reply all
Reply to author
Forward
0 new messages