dimanche 14 mai 2023

Go: Check if pointer is nil or else null value

I have the following problem in Go, for which I am looking for an elegant solution.

I get a struct returned which has the following structure

type PointerStruct struct {
    fieldA *string
    fieldB *string
    ...
}

Now I need to convert it to a similar looking struct:

type ValueStruct struct {
    fieldA string
    fieldB string
    ...
}

So what I need to do is, to dereference every field if it is not nil or put a "" if it is nil. The signature of the function looks like this:

func map(pointerStruct PointerStruct) ValueStruct {}

There are too many fields, so it is not feasible to check every field for nil, the names of the fields are the same in both objects, which should make it easier. There has to be some elegant way to achieve this, any suggestions?





Aucun commentaire:

Enregistrer un commentaire