github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/dump/example_test.go (about)

     1  package dump_test
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  
     7  	"github.com/bingoohuang/gg/pkg/dump"
     8  	"github.com/gookit/color"
     9  	"github.com/kortschak/utter"
    10  	"github.com/kr/pretty"
    11  )
    12  
    13  func ExampleGotcha2() {
    14  	type cc struct {
    15  		a int
    16  		b [3]byte
    17  	}
    18  
    19  	c1 := cc{a: 1, b: [3]byte{1, 2, 3}}
    20  	dump.P(c1)
    21  	// Output:
    22  }
    23  
    24  func ExampleGotcha1() {
    25  	type MyStruct struct {
    26  		A int
    27  	}
    28  
    29  	var wg sync.WaitGroup
    30  
    31  	values := []MyStruct{{1}, {2}, {3}}
    32  	var output []*MyStruct
    33  	for i, v := range values {
    34  		output = append(output, &values[i])
    35  		wg.Add(1)
    36  		go func(m MyStruct) {
    37  			defer wg.Done()
    38  			dump.V(m)
    39  		}(v)
    40  	}
    41  
    42  	dump.V(output)
    43  	wg.Wait()
    44  
    45  	// Output:
    46  }
    47  
    48  func ExampleBasic() {
    49  	dump.P(
    50  		nil, true,
    51  		12, int8(12), int16(12), int32(12), int64(12),
    52  		uint(22), uint8(22), uint16(22), uint32(22), uint64(22),
    53  		float32(23.78), 56.45,
    54  		'c', byte('d'),
    55  		"string",
    56  	)
    57  
    58  	// Output:
    59  	// <nil>,
    60  	// bool(true),
    61  	// int(12),
    62  	// int8(12),
    63  	// int16(12),
    64  	// int32(12),
    65  	// int64(12),
    66  	// uint(22),
    67  	// uint8(22),
    68  	// uint16(22),
    69  	// uint32(22),
    70  	// uint64(22),
    71  	// float32(23.780000686645508),
    72  	// float64(56.45),
    73  	// int32(99),
    74  	// uint8(100),
    75  	// string("string"), #len=6
    76  	//
    77  }
    78  
    79  func ExampleDemo() {
    80  	dump.P(234, int64(56))
    81  	dump.P("abc", "def")
    82  	dump.P([]string{"ab", "cd"})
    83  	dump.P(
    84  		[]interface{}{"ab", 234, []int{1, 3}},
    85  	)
    86  
    87  	// Output:
    88  	// PRINT AT github.com/bingoohuang/gg/pkg/dump.ExampleDemo(example_test.go:37)
    89  	//int(234),
    90  	//int64(56),
    91  	//PRINT AT github.com/bingoohuang/gg/pkg/dump.ExampleDemo(example_test.go:38)
    92  	//string("abc"), #len=3
    93  	//string("def"), #len=3
    94  	//PRINT AT github.com/bingoohuang/gg/pkg/dump.ExampleDemo(example_test.go:39)
    95  	//[]string [ #len=2
    96  	//  string("ab"), #len=2
    97  	//  string("cd"), #len=2
    98  	//],
    99  	//PRINT AT github.com/bingoohuang/gg/pkg/dump.ExampleDemo(example_test.go:40)
   100  	//[]interface {} [ #len=3
   101  	//  string("ab"), #len=2
   102  	//  int(234),
   103  	//  []int [ #len=2
   104  	//    int(1),
   105  	//    int(3),
   106  	//  ],
   107  	//],
   108  }
   109  
   110  func ExampleSlice() {
   111  	dump.P(
   112  		[]byte("abc"),
   113  		[]int{1, 2, 3},
   114  		[]string{"ab", "cd"},
   115  		[]interface{}{
   116  			"ab",
   117  			234,
   118  			[]int{1, 3},
   119  			[]string{"ab", "cd"},
   120  		},
   121  	)
   122  
   123  	// Output:
   124  }
   125  
   126  func ExampleStruct() {
   127  	s1 := &struct {
   128  		cannotExport map[string]interface{}
   129  	}{
   130  		cannotExport: map[string]interface{}{
   131  			"key1": 12,
   132  			"key2": "abcd123",
   133  		},
   134  	}
   135  
   136  	s2 := struct {
   137  		ab string
   138  		Cd int
   139  	}{
   140  		"ab", 23,
   141  	}
   142  
   143  	color.Infoln("- Use fmt.Println:")
   144  	fmt.Println(s1, s2)
   145  
   146  	color.Infoln("\n- Use dump.Println:")
   147  	dump.P(
   148  		s1,
   149  		s2,
   150  	)
   151  
   152  	// Output:
   153  }
   154  
   155  func ExampleMap() {
   156  	dump.P(
   157  		map[string]interface{}{
   158  			"key0": 123,
   159  			"key1": "value1",
   160  			"key2": []int{1, 2, 3},
   161  			"key3": map[string]string{
   162  				"k0": "v0",
   163  				"k1": "v1",
   164  			},
   165  		},
   166  	)
   167  	// Output:
   168  }
   169  
   170  func ExampleDemo2() {
   171  	dump.P(
   172  		23,
   173  		[]string{"ab", "cd"},
   174  		[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
   175  		map[string]interface{}{
   176  			"key": "val", "sub": map[string]string{"k": "v"},
   177  		},
   178  		struct {
   179  			ab string
   180  			Cd int
   181  		}{
   182  			"ab", 23,
   183  		},
   184  	)
   185  
   186  	// Output:
   187  }
   188  
   189  // rum demo:
   190  //
   191  //	go run ./refer_kr_pretty.go
   192  //	go run ./dump/_examples/refer_kr_pretty.go
   193  func ExamleKr() {
   194  	vs := []interface{}{
   195  		23,
   196  		[]string{"ab", "cd"},
   197  		[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, // len > 10
   198  		map[string]interface{}{
   199  			"key": "val", "sub": map[string]string{"k": "v"},
   200  		},
   201  		struct {
   202  			ab string
   203  			Cd int
   204  		}{
   205  			"ab", 23,
   206  		},
   207  	}
   208  
   209  	// print var data
   210  	_, err := pretty.Println(vs...)
   211  	if err != nil {
   212  		panic(err)
   213  	}
   214  
   215  	// print var data
   216  	for _, v := range vs {
   217  		utter.Dump(v)
   218  	}
   219  }