-
Notifications
You must be signed in to change notification settings - Fork 0
/
type-assertion.go
31 lines (27 loc) · 882 Bytes
/
type-assertion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main
import (
"fmt"
"io"
)
type I interface {
m()
}
/*
A type assertion can be used to:
. obtain a value of concrete type from a value of interface type
. obtain a value of a different interface type than the initial one
(an interface with a different method set, practically not subset of the original one as
that could simply be obtained using a simple type conversion).
*/
// See https://stackoverflow.com/questions/31379404/is-this-casting-in-golang
func main() {
var x interface{} = 7 // x has dynamic type int and value 7
i := x.(int) // i has type int and value 7
fmt.Println("x:", x)
fmt.Println("i:", i)
}
func f(y I) {
// s := y.(string) // illegal: string does not implement I (missing method m)
r := y.(io.Reader) // r has type io.Reader and the dynamic type of y must implement both I and io.Reader
fmt.Println("r:", r)
}