https://github.com/drewstinnett/gout
Output go objects in standard formats, such as YAML, JSON, etc
https://github.com/drewstinnett/gout
go golang json output yaml
Last synced: 11 months ago
JSON representation
Output go objects in standard formats, such as YAML, JSON, etc
- Host: GitHub
- URL: https://github.com/drewstinnett/gout
- Owner: drewstinnett
- License: mpl-2.0
- Created: 2021-04-08T20:48:17.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-02-24T22:07:41.000Z (over 2 years ago)
- Last Synced: 2024-05-20T03:22:37.484Z (about 2 years ago)
- Topics: go, golang, json, output, yaml
- Language: Go
- Homepage:
- Size: 155 KB
- Stars: 15
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-extra - go-output-format - 04-08T20:48:17Z|2021-10-18T23:14:38Z| (Bot Building / Markup Languages)
- awesome-go - drewstinnett/gout
README
# GOUT (Previously go-output-format)
[](https://github.com/drewstinnett/gout/actions/workflows/test.yml)
[](https://codecov.io/gh/drewstinnett/gout)
[](https://goreportcard.com/report/github.com/drewstinnett/gout)
[](https://pkg.go.dev/github.com/drewstinnett/gout/v2)
[](https://github.com/avelino/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](https://www.vaultproject.io/),
[az](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli) and
[kubectl](https://kubernetes.io/docs/tasks/tools/)
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
```go
import "github.com/drewstinnett/gout/v2"
```
### Builtin
Gout now comes with a builtin instance, similar to the way Viper does things.
Example Usage:
```go
gout.MustPrint(struct {
FirstName string
LastName string
}{
FirstName: "Bob",
LastName: "Ross",
})
```
Full example code [here](./_examples/builtin/main.go)
### Custom
Example Usage:
```go
// 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](_examples/gotemplate/main.go) for full details.
## Related Projects
* [Gout-Cobra](https://github.com/drewstinnett/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.