当前位置: 首页>>代码示例>>Golang>>正文


Golang Struct.ForEachField方法代码示例

本文整理汇总了Golang中code/google/com/p/go/tools/go/types.Struct.ForEachField方法的典型用法代码示例。如果您正苦于以下问题:Golang Struct.ForEachField方法的具体用法?Golang Struct.ForEachField怎么用?Golang Struct.ForEachField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在code/google/com/p/go/tools/go/types.Struct的用法示例。


在下文中一共展示了Struct.ForEachField方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: findPromotedField

// For a given struct type and (promoted) field Id, findEmbeddedField
// returns the path of implicit anonymous field selections, and the
// field index of the explicit (=outermost) selection.
//
// TODO(gri): if go/types/operand.go's lookupFieldBreadthFirst were to
// record (e.g. call a client-provided callback) the implicit field
// selection path discovered for a particular ast.SelectorExpr, we could
// eliminate this function.
//
func findPromotedField(st *types.Struct, id Id) (*anonFieldPath, int) {
	// visited records the types that have been searched already.
	// Invariant: keys are all *types.Named.
	// (types.Type is not a sound map key in general.)
	visited := make(map[types.Type]bool)

	var list, next []*anonFieldPath
	i := 0
	st.ForEachField(func(f *types.Field) {
		if f.IsAnonymous {
			list = append(list, &anonFieldPath{nil, i, f})
		}
		i++
	})

	// Search the current level if there is any work to do and collect
	// embedded types of the next lower level in the next list.
	for {
		// look for name in all types at this level
		for _, node := range list {
			typ := node.field.Type.Deref().(*types.Named)
			if visited[typ] {
				continue
			}
			visited[typ] = true

			switch typ := typ.Underlying().(type) {
			case *types.Struct:
				for i, n := 0, typ.NumFields(); i < n; i++ {
					f := typ.Field(i)
					if MakeId(f.Name, f.Pkg) == id {
						return node, i
					}
				}
				i := 0
				typ.ForEachField(func(f *types.Field) {
					if f.IsAnonymous {
						next = append(next, &anonFieldPath{node, i, f})
					}
					i++
				})
			}
		}

		if len(next) == 0 {
			panic("field not found: " + id.String())
		}

		// No match so far.
		list, next = next, list[:0] // reuse arrays
	}
	panic("unreachable")
}
开发者ID:pombredanne,项目名称:go.tools,代码行数:62,代码来源:promote.go


注:本文中的code/google/com/p/go/tools/go/types.Struct.ForEachField方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。