You can't. You can only pass a value, and CustomStruct
is not a value but a type. Using a type identifier is a compile-time error.
Usually when a "type" is to be passed, you pass a reflect.Type
value which describes the type. This is what you "create" inside your getTypeName()
, but then the getTypeName()
will have little left to do:
func getTypeName(t reflect.Type) string {
return t.Name()
}
getTypeName(reflect.TypeOf(CustomStruct{}))
(Also don't forget that this returns an empty string for anonymous types such as []int
.)
Another way is to pass a "typed" nil
pointer value as you did, but again, you can just as well use a typed nil
value to create the reflect.Type
too, without creating a value of the type in question, like this:
t := reflect.TypeOf((*CustomStruct)(nil)).Elem()
fmt.Println(t.Name())