I'm looking for a way to hold a type (maybe reflection.Type?) as a field in a custom struct. The reasoning behind this is that I'm decoding JSON arrays into structs from which I later build SQL queries, but ints, floats and timestamps are identical in the JSON array, though they are different when querrying a database. This means I need to convert each value to its correct type before querrying. I think the answer lies somewhere in the reflect package, but I haven't worked out exactly how to utilize it. What I'm hoping for is something like this:
type Column struct {
name string
dataType type
}
someColumn := Column {name: "somecol", dataType: int}
convertedVal := SomeConversionFunc("5", someColumn.dataType)
Alternatively, this sort of thing could work as well:
type Column struct {
name string
dataType func()
}
someColumn := Column {name: "somecol", dataType: ConvertToInt}
convertedVal := someColumn.dataType("5")
Any ideas?
Aucun commentaire:
Enregistrer un commentaire