How to control the way a type is printed out

84 views
Skip to first unread message

notevenhere

unread,
Jan 23, 2024, 5:28:36 PM1/23/24
to golang-nuts
I have some types defined within their respective packages deep inside my project folder structure.

I create nested instances of these classes and print out their types in turn.

These can be seen in the following playground code:


Running the code, one gets

*beta.Beta[*play.ground/structs/alpha.Alpha[float64],*play.ground/structs/alpha.Alpha[string]] 

*alpha.Alpha[*play.ground/structs/beta.Beta[*play.ground/structs/alpha.Alpha[float64],*play.ground/structs/alpha.Alpha[string]]]

If one inspects these types, they will realize that although the outer type is presented without writing out its full path, the inner types are fully qualified type paths.

The problem I am facing is that when I try to write tests of examples that involve some related nested type, I need to type in the full path of the type otherwise if I refactor the project's folder structure, the tests will fail simply because the fully qualified paths to the types has changed but nothing is actually wrong with the code.

So, my question is, how to get eg

*beta.Beta[*alpha.Alpha[float64], *alpha.Alpha[string]]

and

*alpha.Alpha[*beta.Beta[*alpha.Alpha[float64],*alpha.Alpha[string]]]

instead of what I actually get?

My first guess was to use regex's but it feels unnecessarily verbose. Hoping there's something else I'm missing.

Thanks

Def Ceb

unread,
Jan 23, 2024, 6:28:26 PM1/23/24
to golan...@googlegroups.com
Is there any particular reason to compare with %T fmt.Sprintf(?) values
in the first place? If not, then you could just use type assertions. And
the reflect package if really needed.

notevenhere:
> --
> You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to golang-nuts...@googlegroups.com
> <mailto:golang-nuts...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/ae2d2621-b312-4066-9fdd-aaebdbb14622n%40googlegroups.com <https://groups.google.com/d/msgid/golang-nuts/ae2d2621-b312-4066-9fdd-aaebdbb14622n%40googlegroups.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages