README

goon

Build Status GoDoc

Package goon is a deep pretty printer with Go-like notation. It implements the goon specification.

Installation

go get -u github.com/shurcooL/go-goon

Examples

x := Lang{
	Name: "Go",
	Year: 2009,
	URL:  "http",
	Inner: &Inner{
		Field1: "Secret!",
	},
}

goon.Dump(x)

// Output:
// (Lang)(Lang{
// 	Name: (string)("Go"),
// 	Year: (int)(2009),
// 	URL:  (string)("http"),
// 	Inner: (*Inner)(&Inner{
// 		Field1: (string)("Secret!"),
// 		Field2: (int)(0),
// 	}),
// })
items := []int{1, 2, 3}

goon.DumpExpr(len(items))

// Output:
// len(items) = (int)(3)
adderFunc := func(a int, b int) int {
	c := a + b
	return c
}

goon.DumpExpr(adderFunc)

// Output:
// adderFunc = (func(int, int) int)(func(a int, b int) int {
// 	c := a + b
// 	return c
// })

Directories

Path Synopsis
bypass Package bypass allows bypassing reflect restrictions on accessing unexported struct fields.

Alternatives

  • go-spew - A deep pretty printer for Go data structures to aid in debugging.
  • valast - Convert Go values to their AST.
  • repr - Python's repr() for Go.

Attribution

go-goon source was based on the existing source of go-spew by Dave Collins.

License

Documentation

Overview

    Package goon is a deep pretty printer with Go-like notation. It implements the goon specification.

    Example
    Output:
    
    (Lang)(Lang{
    	Name: (string)("Go"),
    	Year: (int)(2009),
    	URL:  (string)("http"),
    	Inner: (*Inner)(&Inner{
    		Field1: (string)("Secret!"),
    		Field2: (int)(0),
    	}),
    })
    
    Example (Arrays)
    Output:
    
    ([0]int)([0]int{})
    ([3]int)([3]int{
    	(int)(1),
    	(int)(2),
    	(int)(3),
    })
    
    Example (Complete)
    Output:
    
    ([]int32)([]int32{
    	(int32)(1),
    	(int32)(5),
    	(int32)(8),
    })
    (*string)(nil)
    (interface{})(nil)
    ([]uint8)([]uint8{
    	(uint8)(102),
    	(uint8)(111),
    	(uint8)(111),
    	(uint8)(100),
    	(uint8)(98),
    	(uint8)(111),
    	(uint8)(111),
    	(uint8)(104),
    	(uint8)(98),
    	(uint8)(105),
    	(uint8)(110),
    	(uint8)(103),
    	(uint8)(98),
    	(uint8)(111),
    	(uint8)(110),
    	(uint8)(103),
    	(uint8)(115),
    	(uint8)(116),
    	(uint8)(114),
    	(uint8)(105),
    	(uint8)(107),
    	(uint8)(101),
    	(uint8)(49),
    	(uint8)(50),
    	(uint8)(51),
    })
    (uintptr)(nil)
    (uintptr)(0x7b)
    (func())(func() { println("This is a func.") })
    (func(int, int) int)(func(a int, b int) int {
    	c := a + b
    	return c
    })
    (struct{ unexportedFunc func() string })(struct{ unexportedFunc func() string }{
    	unexportedFunc: (func() string)(func() string { return "This is the source of an unexported struct field." }),
    })
    
    Example (DumpNamed)
    Output:
    
    somethingImportant = (int)(5)
    somethingImportant = (int)(5)
    somethingImportant = (int)(5)
    
    Example (Large)
    Output:
    
    (*ast.FuncDecl)(&ast.FuncDecl{
    	Doc:  (*ast.CommentGroup)(nil),
    	Recv: (*ast.FieldList)(nil),
    	Name: (*ast.Ident)(&ast.Ident{
    		NamePos: (token.Pos)(115),
    		Name:    (string)("foo"),
    		Obj: (*ast.Object)(&ast.Object{
    			Kind: (ast.ObjKind)(5),
    			Name: (string)("foo"),
    			Decl: (*ast.FuncDecl)(already_shown),
    			Data: (interface{})(nil),
    			Type: (interface{})(nil),
    		}),
    	}),
    	Type: (*ast.FuncType)(&ast.FuncType{
    		Func: (token.Pos)(110),
    		Params: (*ast.FieldList)(&ast.FieldList{
    			Opening: (token.Pos)(118),
    			List: ([]*ast.Field)([]*ast.Field{
    				(*ast.Field)(&ast.Field{
    					Doc: (*ast.CommentGroup)(nil),
    					Names: ([]*ast.Ident)([]*ast.Ident{
    						(*ast.Ident)(&ast.Ident{
    							NamePos: (token.Pos)(119),
    							Name:    (string)("bar"),
    							Obj: (*ast.Object)(&ast.Object{
    								Kind: (ast.ObjKind)(4),
    								Name: (string)("bar"),
    								Decl: (*ast.Field)(already_shown),
    								Data: (interface{})(nil),
    								Type: (interface{})(nil),
    							}),
    						}),
    					}),
    					Type: (*ast.Ident)(&ast.Ident{
    						NamePos: (token.Pos)(123),
    						Name:    (string)("int"),
    						Obj:     (*ast.Object)(nil),
    					}),
    					Tag:     (*ast.BasicLit)(nil),
    					Comment: (*ast.CommentGroup)(nil),
    				}),
    			}),
    			Closing: (token.Pos)(126),
    		}),
    		Results: (*ast.FieldList)(&ast.FieldList{
    			Opening: (token.Pos)(0),
    			List: ([]*ast.Field)([]*ast.Field{
    				(*ast.Field)(&ast.Field{
    					Doc:   (*ast.CommentGroup)(nil),
    					Names: ([]*ast.Ident)(nil),
    					Type: (*ast.Ident)(&ast.Ident{
    						NamePos: (token.Pos)(128),
    						Name:    (string)("int"),
    						Obj:     (*ast.Object)(nil),
    					}),
    					Tag:     (*ast.BasicLit)(nil),
    					Comment: (*ast.CommentGroup)(nil),
    				}),
    			}),
    			Closing: (token.Pos)(0),
    		}),
    	}),
    	Body: (*ast.BlockStmt)(&ast.BlockStmt{
    		Lbrace: (token.Pos)(132),
    		List: ([]ast.Stmt)([]ast.Stmt{
    			(*ast.ReturnStmt)(&ast.ReturnStmt{
    				Return: (token.Pos)(134),
    				Results: ([]ast.Expr)([]ast.Expr{
    					(*ast.BinaryExpr)(&ast.BinaryExpr{
    						X: (*ast.Ident)(&ast.Ident{
    							NamePos: (token.Pos)(141),
    							Name:    (string)("bar"),
    							Obj: (*ast.Object)(&ast.Object{
    								Kind: (ast.ObjKind)(4),
    								Name: (string)("bar"),
    								Decl: (*ast.Field)(&ast.Field{
    									Doc: (*ast.CommentGroup)(nil),
    									Names: ([]*ast.Ident)([]*ast.Ident{
    										(*ast.Ident)(&ast.Ident{
    											NamePos: (token.Pos)(119),
    											Name:    (string)("bar"),
    											Obj:     (*ast.Object)(already_shown),
    										}),
    									}),
    									Type: (*ast.Ident)(&ast.Ident{
    										NamePos: (token.Pos)(123),
    										Name:    (string)("int"),
    										Obj:     (*ast.Object)(nil),
    									}),
    									Tag:     (*ast.BasicLit)(nil),
    									Comment: (*ast.CommentGroup)(nil),
    								}),
    								Data: (interface{})(nil),
    								Type: (interface{})(nil),
    							}),
    						}),
    						OpPos: (token.Pos)(145),
    						Op:    (token.Token)(14),
    						Y: (*ast.BasicLit)(&ast.BasicLit{
    							ValuePos: (token.Pos)(147),
    							Kind:     (token.Token)(5),
    							Value:    (string)("2"),
    						}),
    					}),
    				}),
    			}),
    		}),
    		Rbrace: (token.Pos)(149),
    	}),
    })
    
    Example (Map)
    Output:
    
    (map[string]int64)(map[string]int64{
    	(string)("x"): (int64)(1),
    	(string)("y"): (int64)(4),
    	(string)("z"): (int64)(7),
    })
    
    Example (NilMap)
    Output:
    
    (map[string]int)(nil)
    (map[string]int)(map[string]int{})
    (map[string]int)(map[string]int{
    	(string)("one"): (int)(1),
    })
    
    Example (NilSlice)
    Output:
    
    ([]int)(nil)
    ([]int)([]int{})
    ([]int)([]int{
    	(int)(1),
    })
    
    Example (Time)
    Output:
    
    (time.Time)(time.Date(1, 2, 3, 4, 5, 6, 7, time.UTC))
    (time.Time)(time.Date(1, 2, 3, 4, 5, 6, 7, time.Local))
    (time.Time)(time.Time{})
    
    Example (Unexported)
    Output:
    
    (Lang)(Lang{
    	Name: (string)("Go"),
    	year: (int)(2009),
    	url:  (string)("http"),
    })
    

    Index

    Examples

    Constants

    This section is empty.

    Variables

    This section is empty.

    Functions

    func Dump

    func Dump(a ...interface{}) (n int, err error)

      Dump dumps goons to stdout.

      func DumpExpr

      func DumpExpr(a ...interface{}) (n int, err error)

        DumpExpr dumps goon expressions to stdout.

        E.g., this:

        somethingImportant := 5
        DumpExpr(somethingImportant)
        

        Will print:

        somethingImportant = (int)(5)
        

        func Fdump

        func Fdump(w io.Writer, a ...interface{}) (n int, err error)

          Fdump dumps goons to a writer.

          func FdumpExpr

          func FdumpExpr(w io.Writer, a ...interface{}) (n int, err error)

            FdumpExpr dumps goon expressions to a writer.

            func Sdump

            func Sdump(a ...interface{}) string

              Sdump dumps goons to a string.

              func SdumpExpr

              func SdumpExpr(a ...interface{}) string

                SdumpExpr dumps goon expressions to a string.

                Types

                This section is empty.

                Directories

                Path Synopsis
                Package bypass allows bypassing reflect restrictions on accessing unexported struct fields.
                Package bypass allows bypassing reflect restrictions on accessing unexported struct fields.