In the following code
var r io.Reader
c, _ := net.Dial("tcp", ":8080")
r = c
switch r.(type) {
case io.Reader:
fmt.Println("r implements the reader interface")
// fallthrough
case io.Writer:
fmt.Println("r implements the writer interface")
case net.Conn:
fmt.Println("r implements the conn interface")
}
the first case
statement is the one that's always executed.
If I uncomment fallthrough
the code does not compile given that fallthrough
is not allowed in a type switch
If I am not wrong a go
interface has 2 values:
- the actual TYPE (descriptor) stored in the interface (e.g.
io.Writer
,io.Reader
etc) - the value stored in it
Since r
apparently satisfies all of the above case
statements, what is the way of finding the exact type stored in r
?
Is it only through reflection that this cone be done?