Search code examples
gopointersstructcustom-type

How to assign custom type pointers?


I have:

type MyType string

type Source struct {
  Value *string
}

type Target struct {
  Value *MyType
}

Those are generated types created by a code generator from a JSON schema.

How can I assign Source.Value to Target.Value?

Would this be correct:

var target Target
var source Source

target.Value = new(MyType)
*target.Value = MyType(*source.Value)

Or is there a better way of doing it?


Solution

  • Would this be correct [?]

    Yes.

    (But maybe I miss the point of your question as trying this out would have taken an order of magnitude less time than asking this here on SO.)