go-repr
String representations for Go values. π¬
Installation
go get github.com/hackebrot/go-repr
Usage
The primary feature of go-repr is a function for generating string
representations for Go values: repr.Repr()
. It resolves pointers to their
values and omits unexported struct fields as well as struct fields with nil
values.
Example
package main
import (
"fmt"
"github.com/hackebrot/go-repr/repr"
)
// Emoji holds info about an emoji character
type Emoji struct {
Name string
Category string
Char string
Keywords []string
}
func main() {
fmt.Println(repr.Repr("hello world! π"))
fmt.Println(repr.Repr(1234))
fmt.Println(repr.Repr(true))
astronaut := Emoji{
Name: "woman_astronaut",
Category: "people",
Char: "π©βπ",
Keywords: []string{"space", "rocket", "woman", "human"},
}
fmt.Println(repr.Repr(&astronaut))
}
$ go run example.go
"hello world! π"
1234
true
main.Emoji{Name:"woman_astronaut", Category:"people", Char:"π©βπ", Keywords:["space" "rocket" "woman" "human"]}
About
This project is inspired by github.Stringify()
of
google/github-go and aims at making it easier to work with
structs that use pointer fields. Without repr.Repr()
, you would have to
check for nil
for all of your struct's pointer fields before you can
derefence them one by one.
For more information about go-repr please check out my blog. π
Community
Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given. Please check out this guide to get started!
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
License
Distributed under the terms of the MIT License, go-repr is free and open source software.