From the https://research.swtch.com/interfaces: "To check whether an interface value holds a particular type, as in the type switch above, the Go compiler generates code equivalent to the C expression s.tab->type to obtain the type pointer and check it against the desired type."
What is type pointer and what is the overhead of switching on the type like in this example https://play.golang.org/p/2HIOtPOB1w?
type St struct {
x int
}
func main() {
var i interface{}
i = 12
switch i.(type) {
case int:
fmt.Println("int")
case St:
fmt.Println("St")
}
}
How does type switching (or type assertion) compare to ValueOf in terms of performance and when do I use the latter instead of the former?
To answer your question about overhead in type switches:
I used switches to discriminate underlying types in a value structure in a scripting language I wote. Initially, I stored an integer type constant and switched on that. Later I got curious, and tested using a type switch instead. Both versions had equivelent performance.
This makes sense, since AFAIK, types in an interface are stored as a simple integer constant (which may be a pointer to an internal data structure with more information, but this is not relevent to a simple type check).
I other words, don't worry, it's just as fast as switching on any integer.
I can't vouch for the performance of reflection, but I am willing to bet that using reflection to make a type switch is much slower than just using a type switch.