Type casting in Go

(cinematik) #1


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?

(Norbert Melzer) #2

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

(cinematik) #3

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.

(Norbert Melzer) #4

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.

(cinematik) #6

The reason was in a nil connection.