How to cast a struct to its subclass?

Written by
Link to Post

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.


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

Article Tags:
· ·
Article Categories:

Leave a Reply