英文:
Passing structure's named fields to other functions
问题
这是我的第一个golang
程序,而不仅仅是阅读文档,所以请容忍我。
我有一个结构体,如下所示:(来自解析的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"`
}
现在,在我的主函数中,有一个循环,对每个GLB进行处理:
for _, glb := range config_file.GLBList {
processGLB(glb)
}
processGLB
函数的定义应该是什么样的,才能接收这种类型?
我尝试了以下代码,但它不起作用,我想知道为什么。
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
然后,经过一些搜索,我发现以下代码可以工作。
func processGLB(glb interface{}) {
fmt.Println(glb)
}
这样做是一个好主意吗?为什么我必须使用接口来传递一个简单的结构体字段?
最后,使用golang,有什么最优雅/正确的方法来做到这一点?
编辑:
简单的解决方法是单独定义结构体。
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"`
}
然后,函数定义如下:
func processGLB(glb GLBList) {
// 处理逻辑
}
英文:
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) {
}
答案1
得分: 3
你应该考虑明确定义结构体并重复使用它。使用支持静态类型的语言的全部意义在于尽可能定义你的类型,这有助于编译器发现错误并生成更快的代码。
此外,如果你想要更紧凑的代码,你可以使用匿名字段的概念,尽管我不认为这是最好的情况。
英文:
You should really consider define the struct explicitly and reuse it. The all point of using a langauge with support for static typing is to define your types whenever you can, it helps the compiler to find bugs and produce a faster code.
Also, if you would like to have a more compact code you can use the concept of anonymous fields, although I don't think that is the best scenario for this.
答案2
得分: 1
GLBList被定义为结构体的数组。在Go语言中,没有叫做struct{}的东西。所有的Go类型都实现了空接口,因此可以使用空接口来传递结构体(在这种情况下是没有名称的结构体)。更多详情请参考http://blog.golang.org/json-and-go。
英文:
GLBList is defined as Array of struct. In go, there is nothing called as struct{}. All the go type implements empty interface, hence to pass struct (in this case struct without name) empty interface can be used. For more details http://blog.golang.org/json-and-go
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论