starruler
starruler

Reputation: 302

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

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?

Upvotes: 6

Views: 1012

Answers (1)

Dominik Honnef
Dominik Honnef

Reputation: 18430

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.

Upvotes: 8

Related Questions