mercredi 19 septembre 2018

How to modify a field in a struct of an unknown type?

I have multiple structs that have one common field; let's call it common here

type Struct1 struct {
    foo string
    bar string

    common string
}

type Struct2 struct {
    baz int
    qux string

    common string
}

I want to create a function that takes an Interface as input and nullifies common. The available struct types won't be known at compile time, so I can't create a separate function for each type, and I can't use a switch statement.

P.S: In my use-case, I want to nullify common because it holds the creation time of each struct, and I want to track the history of the struct, so I will know if it changes. Having the creation time inside the struct will mess this up because the creation time will be different every time a new struct is generated even though its actual data may be the same.





Aucun commentaire:

Enregistrer un commentaire