Documentation ¶
Overview ¶
Missing feature of the standard library: printing arbitrary inputs as Go code, with proper spacing and support for multi-line output with indentation. The name "repr" stands for "representation" and alludes to the Python function with the same name.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ConfDefault = Conf{Indent: gg.Indent}
Default config used by top-level formatting functions in this package.
var ConfFull = Conf{Indent: gg.Indent, ZeroFields: true}
Config that allows formatting of struct zero fields.
Functions ¶
func CanBackquote ¶ added in v0.0.4
Corrected version of `strconv.CanBackquote` that allows newlines.
func Println ¶ added in v0.1.0
func Println(src any)
Shortcut for printing the input as Go code, using the default config.
func Prn ¶ added in v0.0.5
Shortcut for printing the input as Go code, prefixed with the given description, using the default config. Handy for debug-printing.
func String ¶
Similar to `fmt.Sprintf("%#v")` or `gg.GoString`, but more advanced. Formats the input as Go code, using the config `ConfDefault`.
Example ¶
package main import ( "fmt" "github.com/mitranim/gg" "github.com/mitranim/gg/grepr" ) type Outer struct { OuterId int OuterName string Embed Inner *Inner } type Embed struct { EmbedId int EmbedName string } type Inner struct { InnerId *int InnerName *string } var testInner = Inner{ InnerId: gg.Ptr(30), InnerName: gg.Ptr(`inner`), } var testEmbed = Embed{EmbedId: 20} var testOuter = Outer{ OuterName: `outer`, Embed: testEmbed, Inner: &testInner, } func main() { fmt.Println(grepr.String(testOuter)) }
Output: grepr_test.Outer{ OuterName: `outer`, Embed: grepr_test.Embed{EmbedId: 20}, Inner: &grepr_test.Inner{ InnerId: gg.Ptr(30), InnerName: gg.Ptr(`inner`), }, }
func StringIndent ¶
Formats the input as Go code, using the default config with the given indentation level.
Types ¶
type Conf ¶
Formatting config.
- `.Indent` controls indentation. If empty, output is single line.
- `.ZeroFields`, if set, forces printing of zero fields in structs. By default zero fields are skipped.
var ConfZero Conf
Empty config for single-line formatting. Note that multi-line strings may still lead to multi-line output. This may change in the future.
func (Conf) AnyString ¶ added in v0.1.0
Shortcut for using this config to format the input as Go code.
func (Conf) IsSingle ¶
Short for "is single line". If `.Indent` is empty, this is true, and output is single-line. Otherwise output is multi-line.