How to cast a struct to its subclass?

Written by
Link to Post

https://www.reddit.com/r/golang/comments/aeorue/how_to_cast_a_struct_to_its_subclass/

Let’s say I have a struct like this: type S struct { x string }

And then I have another struct that embeds (subclasses?) it: type T struct { *S y int }

If a method returns an instance of S, is there any way to convert it to an instance of T? In C++ I would use a copy constructor t = T(s) or deep copy the members. Maybe I’m missing something obvious and the answer is staring me in the face, but I’d like to know what the proper golang way of doing this is.

Thanks!

submitted by /u/DaemonAegis
[link] [comments]

Article Tags:
· ·
Article Categories:
reddit

Leave a Reply