C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Golang program that uses padding with fmt.Printf
package main
import "fmt"
func main() {
values := []string{"bird", "5", "animal"}
// Pad all values to 10 characters.
// ... This right-justifies the strings.
// Three periods just for decoration.
for i := range(values) {
fmt.Printf("%10v...\n", values[i])
}
// Pad all values to 10 characters.
// ... This left-justifies the strings.
// Vertical bars just for decoration.
for i := range(values) {
fmt.Printf("|%-10v|\n", values[i])
}
}
Output
bird...
5...
animal...
|bird |
|5 |
|animal |
Golang program that uses padding with fmt.Sprintf
package main
import "fmt"
func main() {
input := "pad"
// Pad the string and store it in a new string.
padded := fmt.Sprintf("%12v", input)
fmt.Println("Len:", len(padded))
fmt.Println("[" + padded + "]")
}
Output
Len: 12
[ pad]
Tip: Padding works with other format codes like "%d" for numeric codes. Other flags may also be applied in the same format.