I want to write a function that takes different struct-types as 1 parameter. Also, i have to get sure, that in these struct ist a Id
field. so i want to have a function like this: MyFunction(object *struct{ Id int }
I have tried it with passing the struct to a *struct{ Id int }
and to a interface{}
parameter.
For example, i have these 2 struct-types:
type TableOne struct {
Id int
name string
date string
}
type TableTwo struct {
Id int
address string
athome bool
}
To save them in the database (using reflection
) i have the following function:
func SaveMyTables(tablename string, obj *struct{ Id int }) {
// ... Some code here
if obj.Id != 0 {
// ... Some code here
}
// ... Some code here
}
I will call the function like this:
obj := &TableTwo{
Id: 5
address: "some address"
athome: false
}
myPackage.Save("addresses", obj).
But i get this error:
cannot use obj (type *mytables.TableTwo) as type *struct { Id int } in argument to myPackage.Save