我有下面的代码,我正在尝试进行递归。但是,go 编译器抱怨循环引用。当然我有一个循环引用;这是一个递归。
我尝试将递归移入主函数并移出。同样的问题。我怎样才能实现这种递归?错误行是:return lookup(deref.Interface()) // recursion error 'cyclic definition'
christianb@christianb-mac terraform-provider% go build
# github.com/terraform-provider/pkg/foo/repositories.go:773:5: initialization loop:
/Users/christianb/dev/terraform-provider/pkg/foo/repositories.go:773:5: lookup refers to
/Users/christianb/dev/terraform-provider/pkg/foo/repositories.go:774:18: glob..func6.1 refers to
/Users/christianb/dev/terraform-provider/pkg/foo/artifactory/repositories.go:773:5: lookup
type AutoMapper func(field reflect.StructField, thing reflect.Value) map[string]interface{}
var lookup = func() func(payload interface{}) map[string]interface{} {
var handlePtr = func(field reflect.StructField, thing reflect.Value) map[string]interface{} {
deref := reflect.Indirect(thing)
if deref.CanAddr() {
if deref.Kind() == reflect.Struct {
return lookup(deref.Interface()) // recursion error 'cyclic definition'
}
return map[string]interface{}{
field.Tag.Get("hcl"): deref.Interface(),
}
}
return map[string]interface{}{}
}
var checkForHcl = func(mapper AutoMapper) AutoMapper {
return func(field reflect.StructField, thing reflect.Value) map[string]interface{} {
if field.Tag.Get("hcl") != "" {
return mapper(field, thing)
}
return map[string]interface{}{}
}
}
lk := map[reflect.Kind]AutoMapper{}
find := func(payload interface{}) map[string]interface{} {
values := map[string]interface{}{}
var t = reflect.TypeOf(payload)
var v = reflect.ValueOf(payload)
if t.Kind() == reflect.Ptr {
t = t.Elem()
v = v.Elem()
}
茅侃侃
POPMUISE
慕尼黑的夜晚无繁华
相关分类