Skip to content

hackebrot/go-repr

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 

go-repr

GoDoc Reference Build Status

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.

About

String representations for Go values. πŸ’¬

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published