mirror of
https://gitee.com/johng/gf.git
synced 2024-12-03 04:37:49 +08:00
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
package gutil
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/gogf/gf/util/gconv"
|
|
"os"
|
|
"reflect"
|
|
)
|
|
|
|
// Dump prints variables <i...> to stdout with more manually readable.
|
|
func Dump(i ...interface{}) {
|
|
s := Export(i...)
|
|
if s != "" {
|
|
fmt.Println(s)
|
|
}
|
|
}
|
|
|
|
// Export returns variables <i...> as a string with more manually readable.
|
|
func Export(i ...interface{}) string {
|
|
buffer := bytes.NewBuffer(nil)
|
|
for _, v := range i {
|
|
if b, ok := v.([]byte); ok {
|
|
buffer.Write(b)
|
|
} else {
|
|
rv := reflect.ValueOf(v)
|
|
kind := rv.Kind()
|
|
if kind == reflect.Ptr {
|
|
rv = rv.Elem()
|
|
kind = rv.Kind()
|
|
}
|
|
switch kind {
|
|
case reflect.Slice, reflect.Array:
|
|
v = gconv.Interfaces(v)
|
|
case reflect.Map, reflect.Struct:
|
|
v = gconv.Map(v)
|
|
}
|
|
encoder := json.NewEncoder(buffer)
|
|
encoder.SetEscapeHTML(false)
|
|
encoder.SetIndent("", "\t")
|
|
if err := encoder.Encode(v); err != nil {
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
|
}
|
|
}
|
|
}
|
|
return buffer.String()
|
|
}
|