Software Engineering Wiki

PadLeft

Pads the given string from the start with spaces until the resulting string reaches the given length.

  • Use fmt.Sprintf() wih an appropriate format specifier to return the formatted value.
import "fmt"

func PadLeft(s string, l int) string {
	f := "%" + strconv.Itoa(l) + "v"
	return fmt.Sprintf(f, s)
}
PadLeft("go", 8) // "      go"

Last updated 15 September 2026 · Edit this page