I came a cross a piece of code that used .(string)
method. Not knowing what this is called I had difficulties searching for it.
Here is my try to understand it:
package main
import "fmt"
import "reflect"
func main(){
var b interface{}
b = "silly"
fmt.Println(reflect.TypeOf(b.(string))) // we know that b
// is a string
// at compile time
fmt.Println(reflect.TypeOf(b)) // we do not
}
Result:
string
string
However, I think that reflect.TypeOf
takes place at run time, while .(string)
would tell the compiler that b
is indeed a string, and this could be used to tell the compiler that a variable is of certain type. Is my understanding right?