github.com/yaoapp/kun@v0.9.0/utils/utils.go (about)

     1  package utils
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/TylerBrock/colorjson"
     8  	jsoniter "github.com/json-iterator/go"
     9  )
    10  
    11  var json = jsoniter.ConfigCompatibleWithStandardLibrary
    12  
    13  // DD The DD function dumps the given variables and ends execution of the script
    14  func DD(values ...interface{}) {
    15  	Dump(values...)
    16  	os.Exit(0)
    17  }
    18  
    19  // Dump The Dump function dumps the given variables:
    20  func Dump(values ...interface{}) {
    21  	f := colorjson.NewFormatter()
    22  	f.Indent = 4
    23  	for _, v := range values {
    24  		var res interface{}
    25  		if err, ok := v.(error); ok {
    26  			fmt.Printf("%s\n", err.Error())
    27  			continue
    28  		}
    29  
    30  		txt, err := json.Marshal(v)
    31  		if err != nil {
    32  			fmt.Printf("%#v\n%s\n", v, err)
    33  			continue
    34  		}
    35  		json.Unmarshal(txt, &res)
    36  		s, _ := f.Marshal(res)
    37  		fmt.Printf("%s\n", s)
    38  	}
    39  }