samber/go-type-to-string

Incorrect pointer to `interface`

Closed this issue · 9 comments

// Replace output when given type is `any` or `interface{}`, but not a custom interface.
func getInterfaceType(typeOfT reflect.Type) string {

Why?

package main

import (
	"fmt"

	typetostring "github.com/samber/go-type-to-string"
)

func main() {
	type I interface {}

	fmt.Println(typetostring.GetType[I]())   // *main.I
	fmt.Println(typetostring.GetType[*I]())  // *main.I
	fmt.Println(typetostring.GetType[**I]()) // **main.I
}

Without the getInterfaceType hack, typetostring.GetType[interface{}]() would return *interface {}.

This is a know limitation.

If you have a better way to handle this case, I would be very happy to accept your contribution!!

Meant why only for any
What about trim * for all

return t[1:]

We cannot disable all *, beucase we need to differentiate *string et string.

You can disable the getInterfaceType hack and iterate with existing unit test.

Not ALL*, only for interface. when reflect.TypeOf(t) == nil

This behavior wasn't planned, was it?

https://play.golang.com/p/RM9X82ZRprN

@d-enk not planned indeed, and it seems that the previous commit changed the behavior

TBH, I have no opinion on this.

About samber/do, I don't think it makes any difference, since any type is unexpected.

You are confused here

- `any("foobar")` reported as `string` instead of `any` (see #2)

After 1.3.0 there will be any
No try to unpack interface (not only any)

Yes, this is why I listed this in the "not supported yet" section.

I just updated the readme to make it clearer.

OK. I'll add an explanation of reason here too. Just

type I interface {
  // ...
}

The same type as any other (in the context of this package)
which has its own name.