github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/test/go-spew/spew/example_test.go (about)

     1  /*
     2   * Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
     3   *
     4   * Permission to use, copy, modify, and distribute this software for any
     5   * purpose with or without fee is hereby granted, provided that the above
     6   * copyright notice and this permission notice appear in all copies.
     7   *
     8   * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     9   * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    10   * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    11   * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    12   * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    13   * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    14   * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    15   */
    16  
    17  package spew_test
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/davecgh/go-spew/spew"
    23  )
    24  
    25  type Flag int
    26  
    27  const (
    28  	flagOne Flag = iota
    29  	flagTwo
    30  )
    31  
    32  var flagStrings = map[Flag]string{
    33  	flagOne: "flagOne",
    34  	flagTwo: "flagTwo",
    35  }
    36  
    37  func (f Flag) String() string {
    38  	if s, ok := flagStrings[f]; ok {
    39  		return s
    40  	}
    41  	return fmt.Sprintf("Unknown flag (%d)", int(f))
    42  }
    43  
    44  type Bar struct {
    45  	data uintptr
    46  }
    47  
    48  type Foo struct {
    49  	unexportedField Bar
    50  	ExportedField   map[interface{}]interface{}
    51  }
    52  
    53  // This example demonstrates how to use Dump to dump variables to stdout.
    54  func ExampleDump() {
    55  	// The following package level declarations are assumed for this example:
    56  	/*
    57  		type Flag int
    58  
    59  		const (
    60  			flagOne Flag = iota
    61  			flagTwo
    62  		)
    63  
    64  		var flagStrings = map[Flag]string{
    65  			flagOne: "flagOne",
    66  			flagTwo: "flagTwo",
    67  		}
    68  
    69  		func (f Flag) String() string {
    70  			if s, ok := flagStrings[f]; ok {
    71  				return s
    72  			}
    73  			return fmt.Sprintf("Unknown flag (%d)", int(f))
    74  		}
    75  
    76  		type Bar struct {
    77  			data uintptr
    78  		}
    79  
    80  		type Foo struct {
    81  			unexportedField Bar
    82  			ExportedField   map[interface{}]interface{}
    83  		}
    84  	*/
    85  
    86  	// Setup some sample data structures for the example.
    87  	bar := Bar{uintptr(0)}
    88  	s1 := Foo{bar, map[interface{}]interface{}{"one": true}}
    89  	f := Flag(5)
    90  	b := []byte{
    91  		0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
    92  		0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
    93  		0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
    94  		0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
    95  		0x31, 0x32,
    96  	}
    97  
    98  	// Dump!
    99  	spew.Dump(s1, f, b)
   100  
   101  	// Output:
   102  	// (spew_test.Foo) {
   103  	//  unexportedField: (spew_test.Bar) {
   104  	//   data: (uintptr) <nil>
   105  	//  },
   106  	//  ExportedField: (map[interface {}]interface {}) (len=1) {
   107  	//   (string) (len=3) "one": (bool) true
   108  	//  }
   109  	// }
   110  	// (spew_test.Flag) Unknown flag (5)
   111  	// ([]uint8) (len=34 cap=34) {
   112  	//  00000000  11 12 13 14 15 16 17 18  19 1a 1b 1c 1d 1e 1f 20  |............... |
   113  	//  00000010  21 22 23 24 25 26 27 28  29 2a 2b 2c 2d 2e 2f 30  |!"#$%&'()*+,-./0|
   114  	//  00000020  31 32                                             |12|
   115  	// }
   116  	//
   117  }
   118  
   119  // This example demonstrates how to use Printf to display a variable with a
   120  // format string and inline formatting.
   121  func ExamplePrintf() {
   122  	// Create a double pointer to a uint 8.
   123  	ui8 := uint8(5)
   124  	pui8 := &ui8
   125  	ppui8 := &pui8
   126  
   127  	// Create a circular data type.
   128  	type circular struct {
   129  		ui8 uint8
   130  		c   *circular
   131  	}
   132  	c := circular{ui8: 1}
   133  	c.c = &c
   134  
   135  	// Print!
   136  	spew.Printf("ppui8: %v\n", ppui8)
   137  	spew.Printf("circular: %v\n", c)
   138  
   139  	// Output:
   140  	// ppui8: <**>5
   141  	// circular: {1 <*>{1 <*><shown>}}
   142  }
   143  
   144  // This example demonstrates how to use a ConfigState.
   145  func ExampleConfigState() {
   146  	// Modify the indent level of the ConfigState only.  The global
   147  	// configuration is not modified.
   148  	scs := spew.ConfigState{Indent: "\t"}
   149  
   150  	// Output using the ConfigState instance.
   151  	v := map[string]int{"one": 1}
   152  	scs.Printf("v: %v\n", v)
   153  	scs.Dump(v)
   154  
   155  	// Output:
   156  	// v: map[one:1]
   157  	// (map[string]int) (len=1) {
   158  	// 	(string) (len=3) "one": (int) 1
   159  	// }
   160  }
   161  
   162  // This example demonstrates how to use ConfigState.Dump to dump variables to
   163  // stdout
   164  func ExampleConfigState_Dump() {
   165  	// See the top-level Dump example for details on the types used in this
   166  	// example.
   167  
   168  	// Create two ConfigState instances with different indentation.
   169  	scs := spew.ConfigState{Indent: "\t"}
   170  	scs2 := spew.ConfigState{Indent: " "}
   171  
   172  	// Setup some sample data structures for the example.
   173  	bar := Bar{uintptr(0)}
   174  	s1 := Foo{bar, map[interface{}]interface{}{"one": true}}
   175  
   176  	// Dump using the ConfigState instances.
   177  	scs.Dump(s1)
   178  	scs2.Dump(s1)
   179  
   180  	// Output:
   181  	// (spew_test.Foo) {
   182  	// 	unexportedField: (spew_test.Bar) {
   183  	// 		data: (uintptr) <nil>
   184  	// 	},
   185  	// 	ExportedField: (map[interface {}]interface {}) (len=1) {
   186  	//		(string) (len=3) "one": (bool) true
   187  	// 	}
   188  	// }
   189  	// (spew_test.Foo) {
   190  	//  unexportedField: (spew_test.Bar) {
   191  	//   data: (uintptr) <nil>
   192  	//  },
   193  	//  ExportedField: (map[interface {}]interface {}) (len=1) {
   194  	//   (string) (len=3) "one": (bool) true
   195  	//  }
   196  	// }
   197  	//
   198  }
   199  
   200  // This example demonstrates how to use ConfigState.Printf to display a variable
   201  // with a format string and inline formatting.
   202  func ExampleConfigState_Printf() {
   203  	// See the top-level Dump example for details on the types used in this
   204  	// example.
   205  
   206  	// Create two ConfigState instances and modify the method handling of the
   207  	// first ConfigState only.
   208  	scs := spew.NewDefaultConfig()
   209  	scs2 := spew.NewDefaultConfig()
   210  	scs.DisableMethods = true
   211  
   212  	// Alternatively
   213  	// scs := spew.ConfigState{Indent: " ", DisableMethods: true}
   214  	// scs2 := spew.ConfigState{Indent: " "}
   215  
   216  	// This is of type Flag which implements a Stringer and has raw value 1.
   217  	f := flagTwo
   218  
   219  	// Dump using the ConfigState instances.
   220  	scs.Printf("f: %v\n", f)
   221  	scs2.Printf("f: %v\n", f)
   222  
   223  	// Output:
   224  	// f: 1
   225  	// f: flagTwo
   226  }