Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
github.com/ryanuber/columnize
Easy column-formatted output for golang
Columnize is a really small Go package that makes building CLI's a little bit easier. In some CLI designs, you want to output a number similar items in a human-readable way with nicely aligned columns. However, figuring out how wide to make each column is a boring problem to solve and eats your valuable time.
Here is an example:
package main
import (
"fmt"
"github.com/ryanuber/columnize"
)
func main() {
output := []string{
"Name | Gender | Age",
"Bob | Male | 38",
"Sally | Female | 26",
}
result := columnize.SimpleFormat(output)
fmt.Println(result)
}
As you can see, you just pass in a list of strings. And the result:
Name Gender Age
Bob Male 38
Sally Female 26
Columnize is tolerant of missing or empty fields, or even empty lines, so passing in extra lines for spacing should show up as you would expect.
Columnize is configured using a Config
, which can be obtained by calling the
DefaultConfig()
method. You can then tweak the settings in the resulting
Config
:
config := columnize.DefaultConfig()
config.Delim = "|"
config.Glue = " "
config.Prefix = ""
config.Empty = ""
config.NoTrim = false
Delim
is the string by which columns of input are delimitedGlue
is the string by which columns of output are delimitedPrefix
is a string by which each line of output is prefixedEmpty
is a string used to replace blank values found in outputNoTrim
is a boolean used to disable the automatic trimming of input valuesYou can then pass the Config
in using the Format
method (signature below) to
have text formatted to your liking.
See the godoc page for usage.
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.