gotablethis

package module
v0.0.0-...-3e0dc78 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2015 License: Apache-2.0 Imports: 4 Imported by: 0

README

#GoTableThis This Go package will allow you to format and print tables with data. The package is flexible as far as the ways the data can be arranged and the respective output formats.

Overview

In order to print structured information from Golang there are many options to pursue. One option may to leverage a structured medium like XML, JSON, or YAML. The benefit of these methods would that they are readily able to be manipulated programmatically and able to print any length of data predictably.

Otherwise you may want a legacy look and feel, and in this case it may be ideal to format the output in a "pretty table" or "pretty list" with columns and rows and proper divisors.

The actual table formatting aspect is performed from the following TableWriter package.

Tables

There are three direct methods that receive from a Table struct. These methods format the data slightly differently.

Included with the package is the gotablethis_test.go file which can be ran directly with a go test to view the output methods.

Notice how we are sending a reflect Value since this allows us to receive any struct.

PrintColumn

This is the simplest method method where it expects only to see a column name and an array of something that can be converted to strings.

testing := []string{"test1"}

table := Table{
  Header:  []string{"testheader1"},
  RowData: reflect.ValueOf(&testing).Elem(),
}

table.PrintColumn()

This outputs the following table.

+-------------+
| TESTHEADER1 |
+-------------+
| test1       |
+-------------+

PrintKeyValueTable

This method expects that we are sending a single struct with parameters that can be converted to strings. The output is a single table with two columns of key and value.

testing := teststruct{
  Test1: "first",
  Test2: 1,
  Test3: true,
}

table := Table{
  RowData: reflect.ValueOf(&testing).Elem(),
}

table.PrintKeyValueTable()

The output from this method looks like the following.

+-------+-------+
|  KEY  | VALUE |
+-------+-------+
| Test1 | first |
| Test2 | 1     |
| Test3 | true  |
+-------+-------+

PrintTable

In the case that you would like to send a struct with an array of internal structs and choose specific headers you would use PrintTable.

Notice how there is a Header parameter being specified. These names can be arbitrary and map to the Columns data based on ordered position. The Columns array of strings needs to match in a case-sensitive way the parameters being received from the structs.

testing := teststructs{
  teststruct{
    Test1: "first",
    Test2: 1,
    Test3: true,
  },
  teststruct{
    Test1: "second",
    Test2: 2,
    Test3: false,
  },
}

table := Table{
  Header:  []string{"test1", "test2", "test3"},
  Columns: []string{"Test1", "Test2", "Test3"},
  RowData: reflect.ValueOf(&testing).Elem(),
}

table.PrintTable()

Licensing

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Support

Please file bugs and issues at the Github issues page. For more general discussions you can contact the EMC Code team at Google Groups or tagged with EMC on Stackoverflow.com. The code and documentation are released with no warranties or SLAs and are intended to be supported through a community driven process.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyValue

type KeyValue struct {
	Key   string
	Value string
}

KeyValue is stores a Key and a value as interface

type Table

type Table struct {
	Header  []string
	Columns []string
	RowData reflect.Value
}

Table is a special type consisting of header descriptive names and column names and reflected Value with array of structs that have parameters matching any number of column names specified

func (Table) PrintColumn

func (table Table) PrintColumn()

PrintColumn from an array of headers, and a reflected value of a struct with an array of strings

func (Table) PrintKeyValueTable

func (table Table) PrintKeyValueTable()

PrintKeyValueTable from an array of structs

func (Table) PrintTable

func (table Table) PrintTable()

PrintTable from an array of headers, columns and a reflected value of an array of structs

Jump to

Keyboard shortcuts

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