I have many data structures such as :
type dbClusters struct {
some struct members
}
type dbHostStates struct {
some struct members
}
And another struct defined as such:
type tableInfo struct {
tablename string
datastructure []interface{}
}
Somewhere in my code, I use those structs this way:
func myFunc() {
var clusters []dbClusters
var states []dbHostStates
[some code to populate the structs]
tables := tableInfo {
tableInfo { tablename: "clusterstable", datastructure: clusters },
tableInfo { tablename: "statestable", datastructure: states }
}
}
The goal here is later in my code, I’ll have a single function that will be called with []tableInfo as param to process whatever data in the table tableInfo.tablename
, using the data structure in tableInfo.datastructure
.
I thought that having that info in a struct tableInfo
was the proper way, and using an empty interface to map with the actual data structure was “clever”.
The reason I want to go this way is that I might have as 15 tables to handle, I do not wish to duplicate the near-same code 15 times.
What I am missing or misunderstanding here ?
Trying to build, I get this:
[21:43:58|jfgratton@bergen:source]: ./build.sh
# vmman3/db
db/db-import.go:47:54: cannot use hyps (variable of type []dbHypervisors) as type []interface{} in struct literal
db/db-import.go:48:55: cannot use sps (variable of type []dbStoragePools) as type []interface{} in struct literal
db/db-import.go:49:50: cannot use vms (variable of type []dbVmStates) as type []interface{} in struct literal
I understand what it says, not why it says so.