go-goon: github.com/shurcooL/go-goon Index | Examples | Files | Directories

package goon

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

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

Code:

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),
	}),
})

Code:

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),
})

Code:

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." }),
})

Code:

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)

Code:

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
}

Code:

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),
})

Code:

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),
})

Code:

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

Package Files

dump.go goon.go print_types.go

func Dump Uses

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

Dump dumps goons to stdout.

func DumpExpr Uses

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 Uses

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

Fdump dumps goons to a writer.

func FdumpExpr Uses

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

FdumpExpr dumps goon expressions to a writer.

func Sdump Uses

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

Sdump dumps goons to a string.

func SdumpExpr Uses

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

SdumpExpr dumps goon expressions to a string.

Directories

PathSynopsis
bypassPackage bypass allows bypassing reflect restrictions on accessing unexported struct fields.

Package goon imports 9 packages (graph) and is imported by 64 packages. Updated 2016-09-21. Refresh now. Tools for package owners.