I have numerous information frameworks such as:
kind dbClusters struct {
some struct participants.
}
kind dbHostStates struct {
some struct participants.
}
As well as an additional struct specified because of this:
kind tableInfo struct {
tablename string.
datastructure [] user interface {}
}
Someplace in my code, I utilize those structs by doing this:
func myFunc() {
var collections [] dbClusters.
var states [] dbHostStates.
[some code to populate the structs]
tables:= tableInfo {
tableInfo {tablename: "clusterstable", datastructure: collections},.
tableInfo {tablename: "statestable", datastructure: states}
}
}
The objective right here is later on in my code, I’ll have a solitary feature that will certainly be called with [] tableInfo as param to procedure whatever information in the table tableInfo.tablename
, utilizing the information framework in tableInfo.datastructure
I assumed that having that details in a struct tableInfo
was the correct method, as well as utilizing a vacant user interface to map with the real information framework was “smart”.
The factor I intend to go by doing this is that I could have as 15 tables to deal with, I do not desire to replicate the near-same code 15 times.
What I am missing out on or misconstruing right here?
Attempting to develop, I obtain this:
[21:43:58|jfgratton@bergen:source]:./ build.sh.
# vmman3/db.
db/db-import. go:47:54: can not utilize hyps (variable of kind [] dbHypervisors) as kind [] user interface {} in struct actual.
db/db-import. go:48:55: can not utilize sps (variable of kind [] dbStoragePools) as kind [] user interface {} in struct actual.
db/db-import. go:49:50: can not utilize vms (variable of kind [] dbVmStates) as kind [] user interface {} in struct actual.
I comprehend what it states, not why it states so.
Hi!
Golang does not enable straight conversion/assignment of pieces of particular kind to [] user interface {} as discussed right here: Regularly Asked Concerns (FREQUENTLY ASKED QUESTION) – The Go Shows Language
You will certainly require to transform to [] user interface {} initially prior to designating it to datastructure.
func myFunc() {
var collections [] dbClusters.
var states [] dbHostStates.
[some code to populate the structs]
dbC:= make([] user interface {}, len( collections)).
for i, v:= array collections {
dbC[i] = v.
}
dbS:= make([] user interface {}, len( states)).
for i, v:= array states {
dbS[i] = v.
}
tables:= tableInfo {
tableInfo {tablename: "clusterstable", datastructure: dbC},.
tableInfo {tablename: "statestable", datastructure: dbS}
}
}
I have actually located a comparable response as you responded. It type of beats the function of what I was mosting likely to attempt. Since I like Go, until now (me, the Go novice), I think I still need to unlearn some OOP ideas as well as find out the GO method.
From the frequently asked question you have actually given (just how did I miss out on that??), I comprehend the factor underlying this.
This implies then that I’ll some code near-duplication for every single table I need to go through, after that. I intended to develop some type of TableInjectorFactory() feature, however …
Many thanks for the reply @ditchx!
You might additionally discover if generics would certainly help you:
https://gobyexample.com/generics
Easiest method to utilize generics to get rid of replication your code is to utilize a common feature which transforms your pieces to [] user interface {}. The code will certainly currently look something like:
// Common feature to transform pieces of any type of kind to [] user interface {}
func dataStructure[T any]( s [] T) [] user interface {} {
ds:= make([] user interface {}, len( s)).
for i, v:= array s {
ds[i] = v.
}
return ds.
}
func myFunc() {
var collections [] dbClusters.
var states [] dbHostStates.
[some code to populate the structs]
dbC:= dataStructure[dbClusters]( collections).
dbS:= dataStructure[dbHostStates]( states).
tables:= tableInfo {
tableInfo {tablename: "clusterstable", datastructure: dbC},.
tableInfo {tablename: "statestable", datastructure: dbS}
}
}
My understanding of generics is that it is a means to prevent the absence of feature straining in GO, am I right?
I plan to look that a person up in the following version of my software program. This job is my “finding out” job (a Python3 to GO port of a QEMU vm supervisor).
This is interesting until now; many opportunities with GO …
Bookmarked your web link, will most definitely look it up quickly. I intend to obtain this version of my software program out of the door ASAP, after that improve it.
This subject was immediately shut 90 days after the last reply. New replies are no more enabled.