This is my first golang
program rather than just reading docs so please bear with me.
I've a structure like:- (comes from a parsed yaml)
type GLBConfig struct {
GLBList []struct {
Failover string `json:"failover" yaml:"failover"`
GLB string `json:"glb" yaml:"glb"`
Pool []struct {
Fqdn string `json:"fqdn" yaml:"fqdn"`
PercentConsidered int `json:"percent_considered" yaml:"percent_considered"`
} `json:"pool" yaml:"pool"`
} `json:"glb_list" yaml:"glb_list"`
}
Now, in my main function, there's a for loop which does processing of each GLB:-
for _, glb := range config_file.GLBList {
processGLB(glb)
}
What will be the function definition of processGLB
to receive this type?
I tried doing this but it doesn't work and I would like to know why.
func processGLB(glb struct{}) {
fmt.Println(glb)
}
./glb_workers.go:42: cannot use glb (type struct { Failover string "json:\"failover\" yaml:\"failover\""; Glb string "json:\"glb\" yaml:\"glb\""; Pool []struct { Fqdn string "json:\"fqdn\" yaml:\"fqdn\""; PercentConsidered int "json:\"percent_considered\" yaml:\"percent_considered\"" } "json:\"pool\" yaml:\"pool\"" }) as type struct {} in argument to processGLB
Then, some googling later, this works.
func processGLB(glb interface{}) {
fmt.Println(glb)
}
Is it a good idea to do this? Why do I have to use interfaces to pass a simple structure named field?
In the end, what's the most elegant/right way to do this in golang?
EDIT:
Simple solution was to define the structure separately.
type GLBList struct {
Failover string `json:"failover" yaml:"failover"`
GLB string `json:"glb" yaml:"glb"`
Pool []struct {
Fqdn string `json:"fqdn" yaml:"fqdn"`
PercentConsidered int `json:"percent_considered" yaml:"percent_considered"`
} `json:"pool" yaml:"pool"`
}
type GLBConfig struct {
GLBs []GLBList `json:"glb_list" yaml:"glb_list"`
}
And then a function definition like:-
func processGLB(glb GLBList) {
}