"%T" returns 'aurora.value'
kimfucious opened this issue · 6 comments
Hi,
cool package!
This command:
fmt.Println(Sprintf(Green("We can see that the type of x is: %T"), Brown(x)))
Returns:
We can see that the type of x is: aurora.value
I saw the workaround at in the readme for %T
and %p
, but the solution to print the type of x is still eluding me.
Because %T
and %p
don't use fmt.Formatter interface. Solution is, for example:
fmt.Println(
Sprintf(
Green("We can see that the type of x is: %s"),
Brown(fmt.Sprintf("%T", val)),
),
)
Or
func typeOf(val interface{}) string {
return fmt.Sprintf("%T", val)
}
// and
fmt.Println(Sprintf(Green("We can see that the type of x is: %s"), Brown(typeOf(x))))
Also, the fmt.Sprintf("%T", val)
is equal to reflect.TypeOf(val).String()
. Thus the typeOf
function above can be replaced with this one
func typeOf(val interface{}) string {
return reflect.TypeOf(val).String()
}
Or, adding color inside the typeOf
function
func typeOf(val interface{}) string {
return Brown(reflect.TypeOf(val).String()).String()
}
fmt.Println(Sprintf(Green("We can see that the type of x is: %s"), typeOf(x)))
Oh, there is something important: fmt.Sprintf("%T", val)
allows nil, but reflect.TypeOf(val).String()
panics if the val is nil.
Thus, in a middle case fmt.Sprintf("%T", val)
is better, because it's universal.