Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you retrieve the IP address from an open TCPConn*?

Tags:

go

I have a TCPConn* that I've accepted from a TCPListener.acceptTCP. I'd like to get only the IP address (no port information) as a string or IP. What is the correct (i.e. minimal casting/string manipulation) way to accomplish that?

like image 581
starruler Avatar asked Nov 12 '12 04:11

starruler


1 Answers

To get the IP, as an IP object, the most straightforward way should be

tcpconn.RemoteAddr().(*net.TCPAddr).IP

There is nothing wrong with using type assertions, and in cases like this it's actually expected.

like image 88
Dominik Honnef Avatar answered Sep 18 '22 14:09

Dominik Honnef