[Golang] Fixed Width/Length String


Output fixed width/length string in Golang (Go programming language).

Source code (Run Code on Go Playground):

fixedwidth.go | repository | view raw
1
2
3
4
5
6
7
8
package fixedwidth

import "fmt"

func FixedLengthString(length int, str string) string {
	verb := fmt.Sprintf("%%%d.%ds", length, length)
	return fmt.Sprintf(verb, str)
}
fixedwidth_test.go | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package fixedwidth

import "testing"

func TestFixedLengthString(t *testing.T) {
	println(FixedLengthString(20, "This is"))
	println(FixedLengthString(20, "an example of"))
	println(FixedLengthString(20, "fixed width/length"))
	println(FixedLengthString(20, "string"))
}

Tested on: Ubuntu Linux 15.10, Go 1.6.2.


References:

[1]
[2]
[3]Tabby: A tiny library for super simple GoLang tables : golang