func tokenOf(o types.Object) string { switch o := o.(type) { case *types.Func: return "func" case *types.Var: return "var" case *types.TypeName: return "type" case *types.Const: return "const" case *types.PkgName: return "package" case *types.Builtin: return "builtin" // e.g. when describing package "unsafe" case *types.Nil: return "nil" case *types.Label: return "label" case *types.Alias: if o.Orig() == nil { return "alias" } return tokenOf(o.Orig()) } panic(o) }
func formatMember(obj types.Object, maxname int) string { qualifier := types.RelativeTo(obj.Pkg()) var buf bytes.Buffer fmt.Fprintf(&buf, "%-5s %-*s", tokenOf(obj), maxname, obj.Name()) switch obj := obj.(type) { case *types.Alias: buf.WriteString(" => ") if orig := obj.Orig(); orig != nil { fmt.Fprintf(&buf, "%s.%s", orig.Pkg().Name(), orig.Name()) } else { buf.WriteByte('?') } case *types.Const: fmt.Fprintf(&buf, " %s = %s", types.TypeString(obj.Type(), qualifier), obj.Val()) case *types.Func: fmt.Fprintf(&buf, " %s", types.TypeString(obj.Type(), qualifier)) case *types.TypeName: // Abbreviate long aggregate type names. var abbrev string switch t := obj.Type().Underlying().(type) { case *types.Interface: if t.NumMethods() > 1 { abbrev = "interface{...}" } case *types.Struct: if t.NumFields() > 1 { abbrev = "struct{...}" } } if abbrev == "" { fmt.Fprintf(&buf, " %s", types.TypeString(obj.Type().Underlying(), qualifier)) } else { fmt.Fprintf(&buf, " %s", abbrev) } case *types.Var: fmt.Fprintf(&buf, " %s", types.TypeString(obj.Type(), qualifier)) } return buf.String() }