💬 String representations for go values
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
repr
.gitignore
.travis.yml
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE
Makefile
README.md

README.md

go-repr

GoDoc Reference Build Status

String representations for go values

Installation

go get github.com/hackebrot/go-repr

Usage

Import go-repr and then use repr.Repr() to create a string representation for a value. It resolves pointers to their values and omits unexported struct fields as well as struct fields with nil values.

repr.Repr("hello world!")

repr.Repr(1234)

type Gopher struct {
    Hair *string
    Eyes *string
}

e := "Goofy Eyes"
g := &Gopher{Eyes: &e}

repr.Repr(g)

Example

package main

import (
	"fmt"
	"time"

	"github.com/hackebrot/go-repr/repr"
)

// Maintainer of a Project
type Maintainer struct {
	Name *string
}

// Release represents a version of a Project
type Release struct {
	PublishedAt time.Time
	Version     string
}

// Project represents a OSS project
type Project struct {
	Name        *string
	Description *string
	Maintainers []*Maintainer
	Forks       *int
	url         *string
	Platform    map[string]bool
	Releases    []*Release
}

func main() {
	aName := "Amelia"
	amelia := &Maintainer{Name: &aName}

	tName := "Tony"
	tony := &Maintainer{Name: &tName}

	pName := "Chew"
	pForks := 550

	p := &Project{
		Name:        &pName,
		Maintainers: []*Maintainer{amelia, tony},
		Forks:       &pForks,
		Platform:    map[string]bool{"linux": true, "windows": true, "osx": true},
		Releases: []*Release{
			&Release{
				PublishedAt: time.Date(2016, 01, 01, 01, 04, 10, 0, time.UTC),
				Version:     "0.1.0",
			},
			&Release{
				PublishedAt: time.Date(2017, 01, 02, 15, 04, 05, 0, time.UTC),
				Version:     "1.0.0",
			},
		},
	}

	fmt.Printf("%v\n", repr.Repr(p))
}

Above code will generate the following output:

main.Project{Name:"Chew", Maintainers:[main.Maintainer{Name:"Amelia"}
main.Maintainer{Name:"Tony"}], Forks:550, Platform:map["windows":true "osx":true 
"linux":true], Releases:[main.Release{PublishedAt:time.Time{2016-01-01 01:04:10 
+0000 UTC}, Version:"0.1.0"} main.Release{PublishedAt:time.Time{2017-01-02 
15:04:05 +0000 UTC}, Version:"1.0.0"}]}

About

go-repr is inspired by github.Stringify() of google/github-go and aims at making it easier to work with structs that use pointer fields. Without github.Stringify() or repr.Repr() respectively, you would have to check for nil for all of your struct's pointer fields before you can derefence them one by one.

go-repr allows you to debug print your values with a single line of code.

Read more on my blog.

License

Distributed under the terms of the MIT License, go-repr is free and open source software.

Contributing

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!

Code of Conduct

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.