Valast converts Go values at runtime into their go/ast
equivalent, e.g.:
x := &foo.Bar{
a: "hello world!",
B: 1.234,
}
fmt.Println(valast.String(x))
Prints string:
&foo.Bar{a: "hello world!", B: 1.234}
What is this useful for?
This can be useful for debugging and testing, you may think of it as a more comprehensive and configurable version of the fmt
package's %+v
and %#v
formatting directives.
Alternatives
The following are alternatives to valast:
- github.com/davecgh/go-spew (may be inactive, produces a Go-like syntax but not exactly Go syntax)
- github.com/shurcooL/go-goon (based on go-spew, produces valid Go syntax but not via a
go/ast
, produces less idiomatic/terse results)
You may also wish to look at autogold and go-cmp, which aim to solve the "compare Go values in a test" problem.