goon

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2022 License: MIT Imports: 10 Imported by: 199

README

goon

Go Reference

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

Deprecated: This package is old, incomplete, low code quality, and now unmaintained. See github.com/hexops/valast for a newer package that is the closest known direct replacement. See the Alternatives section for other known entries in this problem space.

Installation

go get 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.

Deprecated: This package is old, incomplete, low code quality, and now unmaintained. See github.com/hexops/valast for a newer package that is the closest known direct replacement. See the Alternatives section in README.md for other known entries in this problem space.

Example
type Inner struct {
	Field1 string
	Field2 int
}
type Lang struct {
	Name  string
	Year  int
	URL   string
	Inner *Inner
}

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),
	}),
})
Example (Arrays)
var x [0]int
var y = [...]int{1, 2, 3}

goon.Dump(x)
goon.Dump(y)
Output:

([0]int)([0]int{})
([3]int)([3]int{
	(int)(1),
	(int)(2),
	(int)(3),
})
Example (Complete)
goon.Dump([]int32{1, 5, 8})

{
	x := (*string)(nil)
	goon.Dump(x, nil)
}

goon.Dump([]byte("foodboohbingbongstrike123"))

goon.Dump(uintptr(0), uintptr(123))

{
	f := func() { println("This is a func.") }

	goon.Dump(f)

	f2 := func(a int, b int) int {
		c := a + b
		return c
	}

	goon.Dump(f2)

	unexportedFuncStruct := struct {
		unexportedFunc func() string
	}{func() string { return "This is the source of an unexported struct field." }}

	goon.Dump(unexportedFuncStruct)
}
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)
somethingImportant := 5
goon.DumpExpr(somethingImportant)
fmt.Print(goon.SdumpExpr(somethingImportant))
goon.FdumpExpr(os.Stdout, somethingImportant)
Output:

somethingImportant = (int)(5)
somethingImportant = (int)(5)
somethingImportant = (int)(5)
Example (Large)
package main

import (
	"go/ast"
	"go/parser"
	"go/token"
	"runtime"

	"github.com/shurcooL/go-goon"
)

func foo(bar int) int { return bar * 2 }

func main() {
	fset := token.NewFileSet()
	if file, err := parser.ParseFile(fset, thisGoSourceFile(), nil, 0); nil == err {
		for _, d := range file.Decls {
			if f, ok := d.(*ast.FuncDecl); ok {
				goon.Dump(f)
				break
			}
		}
	}

}

// thisGoSourceFile returns the full path of the Go source file where this function was called from.
func thisGoSourceFile() string {
	_, file, _, _ := runtime.Caller(1)
	return file
}
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)
goon.Dump(map[string]int64{
	"x": 1,
	"y": 4,
	"z": 7,
})
Output:

(map[string]int64)(map[string]int64{
	(string)("x"): (int64)(1),
	(string)("y"): (int64)(4),
	(string)("z"): (int64)(7),
})
Example (NilMap)
var x map[string]int = nil
var y map[string]int = map[string]int{}
var z map[string]int = map[string]int{"one": 1}

goon.Dump(x)
goon.Dump(y)
goon.Dump(z)
Output:

(map[string]int)(nil)
(map[string]int)(map[string]int{})
(map[string]int)(map[string]int{
	(string)("one"): (int)(1),
})
Example (NilSlice)
var x []int = nil
var y []int = []int{}
var z []int = []int{1}

goon.Dump(x)
goon.Dump(y)
goon.Dump(z)
Output:

([]int)(nil)
([]int)([]int{})
([]int)([]int{
	(int)(1),
})
Example (Time)
goon.Dump(time.Date(1, 2, 3, 4, 5, 6, 7, time.UTC))
goon.Dump(time.Date(1, 2, 3, 4, 5, 6, 7, time.Local))
goon.Dump(time.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)
type Lang struct {
	Name string
	year int
	url  string
}

x := Lang{
	Name: "Go",
	year: 2009,
	url:  "http",
}

goon.Dump(x)
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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL