Reputation: 10846
I'm wondering how can you know if the interface is of type pointer.
package main
import "fmt"
import "reflect"
type str struct {
a, b string
}
func main() {
var s str
x := &s
t := reflect.TypeOf(interface{}(x))
fmt.Printf("%v", t.Size())
}
Upvotes: 0
Views: 65
Reputation: 99234
Use a type switch if you already know the type(s):
switch v.(type) {
case *str:
return "*str"
case str:
return "str"
}
If you don't, then you can use if reflect.TypeOf(v).Kind() == reflect.Ptr {}
Upvotes: 6