英文:
iterate over all interface methods in go
问题
如何对一个 XML 文件运行一系列检查?每个检查都是一个返回两个字符串的方法:它的名称和通过或失败的结果。我尝试将所有的方法放在一个接口中,但是无法弄清如何进行迭代。
在这个代码示例中,尝试为注释"WANT"找到一些可行的解决方案:
package main
import (
"fmt"
"github.com/antchfx/xmlquery"
)
type node xmlquery.Node
type XmlChecks interface {
checkUTC() (string, string)
checkSugPresDel() (string, string)
checkStartNum() (string, string)
// ... 还会有很多其他方法
}
type XmlVerify struct {
doc node
}
func (xver XmlVerify) checkUTC() (string, string) {
//TBD
return "cUTC", "pass"
}
func (xver XmlVerify) checkSugPresDel() (string, string) {
//TBD
return "cSugPresDel", "pass"
}
func (xver XmlVerify) checkStartNum() (string, string) {
//TBD
return "cStartNum", "pass"
}
func main() {
var vrfy XmlVerify
vrfy.doc, _ := xmlquery.Parse("myfile.xml")
for each_method := range "vrfy.XmlChecks 中的所有方法" { // <--WANT
fmt.Printf("%s %s\n", vrfy.each_method()) // <--WANT
}
}
英文:
How to run a suite of checks on an xml file? Each check is a method
that returns 2 strings: it's name and pass or fail.
Using xmlquery. I tried putting all the methods in an interface but
can't figure out how to iterate.
In this code sample, trying to get something to work for comment "WANT":
package main
import (
"fmt"
"github.com/antchfx/xmlquery"
)
type node xmlquery.Node
type XmlChecks interface {
checkUTC() (string, string)
checkSugPresDel() (string, string)
checkStartNum() (string, string)
// ... there will be many
}
type XmlVerify struct {
doc node
}
func (xver XmlVerify) checkUTC() (string, string) {
//TBD
return "cUTC", "pass"
}
func (xver XmlVerify) checkSugPresDel() (string, string) {
//TBD
return "cSugPresDel", "pass"
}
func (xver XmlVerify) checkStartNum() (string, string) {
//TBD
return "cStartNum", "pass"
}
func main() {
var vrfy XmlVerify
vrfy.doc, _ := xmlquery.Parse("myfile.xml")
for each_method := range "all the methods in vrfy.XmlChecks" { //<--WANT
fmt.Printf("%s %s\n", vrfy.each_method()) //<--WANT
}
}
答案1
得分: 2
这是一个使用reflect
包的示例,展示了如何进行操作。只需更改XmlVerify
内部的值,就可以使示例变得更简单一些。还要确保方法名是导出的(首字母大写)。
package main
import (
"fmt"
"reflect"
)
type XmlVerify struct {
value string
}
func (xver XmlVerify) CheckUTC() (string, bool) {
return "cUTC", xver.value == "one"
}
func (xver XmlVerify) CheckSugPresDel() (string, bool) {
return "cSugPresDel", xver.value == "two"
}
func (xver XmlVerify) CheckStartNum() (string, bool) {
return "cStartNum", xver.value == "three"
}
func verify(xmlverify XmlVerify) {
t := reflect.TypeOf(xmlverify)
for i := 0; i < t.NumMethod(); i++ {
method := t.Method(i)
ret := reflect.ValueOf(xmlverify).MethodByName(method.Name).Call(nil)
fmt.Printf("%s checks passed: %t\n", ret[0], ret[1])
}
}
func main() {
v := XmlVerify{"two"}
verify(v)
}
输出结果:
cStartNum checks passed: false
cSugPresDel checks passed: true
cUTC checks passed: false
英文:
Here is an example of how you can do it with reflect
. Only changed the value inside XmlVerify to make the example a bit easier. Also make sure the method names are exported (capitalize).
package main
import (
"fmt"
"reflect"
)
type XmlVerify struct {
value string
}
func (xver XmlVerify) CheckUTC() (string, bool) {
return "cUTC", xver.value == "one"
}
func (xver XmlVerify) CheckSugPresDel() (string, bool) {
return "cSugPresDel", xver.value == "two"
}
func (xver XmlVerify) CheckStartNum() (string, bool) {
return "cStartNum", xver.value == "three"
}
func verify(xmlverify XmlVerify) {
t := reflect.TypeOf(xmlverify)
for i := 0; i < t.NumMethod(); i++ {
method := t.Method(i)
ret := reflect.ValueOf(xmlverify).MethodByName(method.Name).Call(nil)
fmt.Printf("%s checks passed: %t\n", ret[0], ret[1])
}
}
func main() {
v := XmlVerify{"two"}
verify(v)
}
output:
cStartNum checks passed: false
cSugPresDel checks passed: true
cUTC checks passed: false
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论