Type casting in Go

https://play.golang.org/p/GoiTRYocKkg

It seems the code doesn’t work as expected because I didn’t get the

r is of type Writer

So what’s wrong with it?

The type Con implements Reader and Writer interfaces, but for some reason I get the message from if statement only if I write !ok - which is actually false, but why should it be false if it must be true?

Yes, it implements Write, but it is not of type Write, the type is net.Conn.

Well, should the the result of reflection be true? The reason why do I rise the topic is because I now passing a course where some guy running the code and gives the true as a result.

Thats not a reflection, thats a typeguard. A typeguard checks for the exact type and happens at compile time. Reflection is a runtime thing and uses the facilities provided by reflect-package.

Aside of that: c is nil due to the connection can’t be established, perhaps thats related as well. Please make sure you check a port that actually is open for connections.

2 Likes

The reason was in a nil connection.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.