colprint

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2017 License: MIT Imports: 10 Imported by: 1

README

Colprint

Easy column formatted printing of structs and slices/arrays of structs in golang.

Build Status GoDoc

Colprint is a small Go package to help build CLI appliactions where you want to list items in human readable form in formatted columns. Colprint builds on Columnize, and adds functionality to easy print structs and slices/arrays of structs. You just have have to add the colprint tag to the fields you want to print.

Installation

Using Colprint is easy. First, use go get to install the latest version of the library.

$ go get github.com/peteabre/colprint

Next, include Colprint in your application:

import "github.com/peteabre/colprint"

Getting started

After installing the library and including it in you application, simply tag your structs and pass them as arguments to the Print function. The tag takes two arguments:

  1. Label - column header (string)
  2. Order - print order of column (int)

Example:

package main

import "github.com/peteabre/colprint"

type Person struct {
        FirstName string `colprint:"First name,1"`
        LastName string  `colprint:"Last name,2"`
        Age int          `colprint:"Age,3"`
} 

func main()  {
        persons := []Person{
                {
                        FirstName: "Ola",
                        LastName:  "Nordmann",
                        Age:        35,
                },
                {
                        FirstName: "Kari",
                        LastName:  "Nordmann",
                        Age:        37,
                },
         }
         colprint.Print(persons)
}

As you can see, if you have a tagged struct, you can simply pass a slice/array and the result will be:

First name  Last name  Age
Ola         Nordmann   35
Kari        Nordmann   37

Documentation

Index

Constants

View Source
const (
	TagValueEmpty    = ""
	TagValueSkip     = "-"
	TagValueTraverse = "=>"
)
View Source
const TagName = "colprint"

Variables

This section is empty.

Functions

func Fprint

func Fprint(w io.Writer, s interface{}, c ...*Config) error

Fprint prints struct or slice to provided io.Writer using provided config. If config is nil, default config will be used.

func Print

func Print(s interface{}) error

Print prints a struct or slice of structs to stdout using default config

func Sprint added in v1.0.1

func Sprint(s interface{}) (string, error)

Sprint is a convenience method for creating a string from a struct or slice of structs using default config

Types

type Config

type Config struct {
	// MaxPrintedSliceItems represents the maximum number og slice items to list.
	MaxPrintedSliceItems *int
	// FloatPrecision represents the precision used when printing floats.
	FloatPrecision *int
}

Config holds configuration used when printing columns

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL