hello how can i convert net.Addr to *net.TCPAddr

2,254 views
Skip to first unread message

Tao Yinqing

unread,
Aug 19, 2011, 2:34:39 AM8/19/11
to golang-nuts

I want to get the addr of a net.TCPConn
but the net.TCPConn method RemoteAddr return the interface  net.Addr
How can i convert the net.Addr to  *net.TCPAddr

thanks 


--
Tao Yinqing (陶寅清)

Miek Gieben

unread,
Aug 19, 2011, 3:13:26 AM8/19/11
to golang-nuts
[ Quoting Tao Yinqing at 14:34 on August 19 in "[go-nuts] hello how can i convert n"... ]

>
> I want to get the addr of a net.TCPConn
> but the net.TCPConn method RemoteAddr return the interface  net.Addr
> How can i convert the net.Addr to  *net.TCPAddr

If the net.Addr is stored in the variable 'a', you should be able
to use a type switch:

a.(*net.TCPAddr)

grtz,

--
Miek

signature.asc

Tao Yinqing

unread,
Aug 19, 2011, 3:23:44 AM8/19/11
to golang-nuts
thanks
--
Tao Yinqing (陶寅清)

Jesse McNelis

unread,
Aug 19, 2011, 5:10:15 AM8/19/11
to golang-nuts

I'd just like to point out that's not a "type switch"
it's a "type assertion".

A "type switch" is different
http://golang.org/doc/go_spec.html#Switch_statements

Miek Gieben

unread,
Aug 19, 2011, 5:14:12 AM8/19/11
to golang-nuts
[ Quoting Jesse McNelis at 19:10 on August 19 in "Re: [go-nuts] hello how can i conve"... ]

> >If the net.Addr is stored in the variable 'a', you should be able
> >to use a type switch:
> >
> > a.(*net.TCPAddr)
> >
> > grtz,
> >
> >--
> > Miek
>
> I'd just like to point out that's not a "type switch"
> it's a "type assertion".
>
> A "type switch" is different
> http://golang.org/doc/go_spec.html#Switch_statements

Ah, thanks. I knew that when typing the message, but couldn't remember
it on the spot.

grtz,

--
Miek

signature.asc
Reply all
Reply to author
Forward
0 new messages