Coder Social home page Coder Social logo

gout's Introduction

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.

gout's People

Contributors

drewstinnett avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.