本文整理汇总了Golang中github.com/cznic/strutil.Formatter类的典型用法代码示例。如果您正苦于以下问题:Golang Formatter类的具体用法?Golang Formatter怎么用?Golang Formatter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Formatter类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: explain
func (s *selectStmt) explain(ctx *execCtx, w strutil.Formatter) {
p, err := s.plan(ctx)
if err != nil {
w.Format("ERROR: %v\n", err)
return
}
p.explain(w)
}
示例2: prettyPrint0
func prettyPrint0(protect map[interface{}]struct{}, sf strutil.Formatter, prefix, suffix string, v interface{}) {
if v == nil {
return
}
switch x := v.(type) {
case *Token:
if x == nil {
return
}
sf.Format("%s%v"+suffix, prefix, x.String())
return
}
rt := reflect.TypeOf(v)
rv := reflect.ValueOf(v)
switch rt.Kind() {
case reflect.Slice:
if rv.Len() == 0 {
return
}
sf.Format("%s[]%T{ // len %d%i\n", prefix, rv.Index(0).Interface(), rv.Len())
for i := 0; i < rv.Len(); i++ {
prettyPrint0(protect, sf, fmt.Sprintf("%d: ", i), ",\n", rv.Index(i).Interface())
}
sf.Format("%u}" + suffix)
case reflect.Struct:
sf.Format("%s%T{%i\n", prefix, v)
for i := 0; i < rt.NumField(); i++ {
f := rv.Field(i)
if !f.CanInterface() {
continue
}
prettyPrint0(protect, sf, fmt.Sprintf("%s: ", rt.Field(i).Name), ",\n", f.Interface())
}
sf.Format("%u}" + suffix)
case reflect.Ptr:
if rv.IsNil() {
return
}
rvi := rv.Interface()
if _, ok := protect[rvi]; ok {
sf.Format("%s&%T{ /* recursive/repetitive pointee not shown */ }"+suffix, prefix, rv.Elem().Interface())
return
}
if protect == nil {
protect = map[interface{}]struct{}{}
}
protect[rvi] = struct{}{}
prettyPrint0(protect, sf, prefix+"&", suffix, rv.Elem().Interface())
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int8:
if v := rv.Int(); v != 0 {
sf.Format("%s%v"+suffix, prefix, v)
}
case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint8:
if v := rv.Uint(); v != 0 {
sf.Format("%s%v"+suffix, prefix, rv.Uint())
}
case reflect.Bool:
if v := rv.Bool(); v {
sf.Format("%s%v"+suffix, prefix, rv.Bool())
}
case reflect.String:
s := rv.Interface().(string)
if s == "" {
return
}
sf.Format("%s%q"+suffix, prefix, s)
case reflect.Map:
keys := rv.MapKeys()
if len(keys) == 0 {
return
}
var buf bytes.Buffer
nf := strutil.IndentFormatter(&buf, "· ")
var skeys []string
for i, k := range keys {
prettyPrint0(protect, nf, "", "", k.Interface())
skeys = append(skeys, fmt.Sprintf("%s%10d", buf.Bytes(), i))
}
sort.Strings(skeys)
sf.Format("%s%T{%i\n", prefix, v)
for _, k := range skeys {
si := strings.TrimSpace(k[len(k)-10:])
k = k[:len(k)-10]
n, _ := strconv.ParseUint(si, 10, 64)
mv := rv.MapIndex(keys[n])
prettyPrint0(protect, sf, fmt.Sprintf("%s: ", k), ",\n", mv.Interface())
}
sf.Format("%u}" + suffix)
default:
panic(fmt.Sprintf("prettyPrint: missing support for reflect.Kind == %v", rt.Kind()))
}
//.........这里部分代码省略.........