columnize.go
func Format(lines []string, config *Config) string {
	conf := MergeConfig(DefaultConfig(), config)
	widths := widthsFromLines(conf, lines)

	// Estimate the buffer size
	glueSize := len(conf.Glue)
	var size int
	for _, w := range widths {
		size += w + glueSize
	}
	size *= len(lines)

	// Create the buffer
	buf := bytes.NewBuffer(make([]byte, 0, size))

	// Create a cache for the string formats
	fmtCache := make(map[int]string, 16)

	// Create the formatted output using the format string
	for _, line := range lines {
		elems := elementsFromLine(conf, line)

		// Get the string format using cache
		numElems := len(elems)
		stringfmt, ok := fmtCache[numElems]
		if !ok {
			stringfmt = stringFormat(conf, widths, numElems)
			fmtCache[numElems] = stringfmt
		}

		fmt.Fprintf(buf, stringfmt, elems...)
	}

	// Get the string result
	result := buf.String()

	// Remove trailing newline without removing leading/trailing space
	if n := len(result); n > 0 && result[n-1] == '\n' {
		result = result[:n-1]
	}

	return result
}
func Format(lines []string, config *Config) string {
	conf := MergeConfig(DefaultConfig(), config)
	widths := widthsFromLines(conf, lines)

	// Estimate the buffer size
	glueSize := len(conf.Glue)
	var size int
	for _, w := range widths {
		size += w + glueSize
	}
	size *= len(lines)

	// Create the buffer
	buf := bytes.NewBuffer(make([]byte, 0, size))

	// Create a cache for the string formats
	fmtCache := make(map[int]string, 16)

	// Create the formatted output using the format string
	for _, line := range lines {
		elems := elementsFromLine(conf, line)

		// Get the string format using cache
		numElems := len(elems)
		stringfmt, ok := fmtCache[numElems]
		if !ok {
			stringfmt = stringFormat(conf, widths, numElems)
			fmtCache[numElems] = stringfmt
		}

		fmt.Fprintf(buf, stringfmt, elems...)
	}

	// Get the string result
	result := buf.String()

	// Remove trailing newline without removing leading/trailing space
	if n := len(result); n > 0 && result[n-1] == '\n' {
		result = result[:n-1]
	}

	return result
}
func Format ( lines [ ] string , config * Config ) string { conf := MergeConfig ( DefaultConfig ( ) , config ) widths := widthsFromLines ( conf , lines ) // Estimate the buffer size glueSize := len ( conf . Glue ) var size int for _ , w := range widths { size += w + glueSize } size *= len ( lines ) // Create the buffer buf := bytes . NewBuffer ( make ( [ ] byte , 0 , size ) ) // Create a cache for the string formats fmtCache := make ( map [ int ] string , 16 ) // Create the formatted output using the format string for _ , line := range lines { elems := elementsFromLine ( conf , line ) // Get the string format using cache numElems := len ( elems ) stringfmt , ok := fmtCache [ numElems ] if ! ok { stringfmt = stringFormat ( conf , widths , numElems ) fmtCache [ numElems ] = stringfmt } fmt . Fprintf ( buf , stringfmt , elems ... ) } // Get the string result result := buf . String ( ) // Remove trailing newline without removing leading/trailing space if n := len ( result ) ; n > 0 && result [ n - 1 ] == '\n' { result = result [ : n - 1 ] } return result }
// Format is the public-facing interface that takes a list of strings and
// returns nicely aligned column-formatted text.
Format is the public - facing interface that takes a list of strings and returns nicely aligned column - formatted text .