• Stars
    star
    13
  • Rank 1,463,177 (Top 30 %)
  • Language
    Go
  • License
    Mozilla Public Li...
  • Created about 3 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Output go objects in standard formats, such as YAML, JSON, etc

GOUT (Previously go-output-format)

Test codecov Go Report Card Go Reference Mentioned in Awesome Go

gout is the Go OUTput Formatter for serializing data in a standard way.

Helper utility to output data structures in to standardized formats, much like what is built in to vault, az and kubectl

I really like how these apps provide for flexible output, but wanted a way to do it without needing to re-write or copy it for every new tool.

Need to parse some output with jq? JSON is your format. Want to put it out in an easy to read yet still standardized format? YAML is for you!

This tool is intended to provide all that in a single reusable package.

Usage

import "github.com/drewstinnett/gout/v2"

Builtin

Gout now comes with a builtin instance, similar to the way Viper does things.

Example Usage:

gout.MustPrint(struct {
  FirstName string
  LastName  string
}{
  FirstName: "Bob",
  LastName:  "Ross",
})

Full example code here

Custom

Example Usage:

// Create a new instance
w, err := gout.New()
if err != nil {
  panic(err)
}

// Use a custom writer
w.SetWriter(os.Stderr)

// Print something!
w.MustPrint(struct {
  FirstName string
  LastName  string
}{
  FirstName: "Bob",
  LastName:  "Ross",
})
// {"FirstName":"Bob","LastName":"Ross"}

Built-in Formatters

YAML

Uses the standard gopkg.in/yaml.v3 library.

JSON

Uses the standard encoding/json library.

TOML

Uses github.com/pelletier/go-toml/v2 library

CSV

Uses github.com/jszwec/csvutil library. NOTE: You must pass an iterable interface in when using this format. It won't do a single struct.

XML

Uses encoding/xml library. NOTE: This plugin only works with structs supported by the base library

Plain

This is just vanilla old Golang output, using the %+v format.

GoTemplate

Use this format to parse the data in to a golang template. Useful for spitting data out in a more arbitrary format. This uses the text/template package to parse each item in the return slice. See the example here for full details.

Related Projects

  • Gout-Cobra - Configure a cobra.Command to output using Gout

Coming Soon?

TSV (Tab Separated Values)

Intention here is to have a simple way to print out a data structure in a way that grep and the like can parse it.