Use strings.Repeat to generate padding and concatenate it with your string, or use fmt.Sprintf with width specifiers for a cleaner approach.
import "fmt"
s := "hello"
// Left pad to 10 chars with spaces
leftPadded := fmt.Sprintf("%10s", s)
// Right pad to 10 chars with spaces
rightPadded := fmt.Sprintf("%-10s", s)
// Custom padding (e.g., zeros)
leftZero := fmt.Sprintf("%010s", s)
For manual control without fmt, calculate the needed length and use strings.Repeat:
import "strings"
s := "hello"
width := 10
pad := " "
// Left pad
leftPadded := strings.Repeat(pad, width-len(s)) + s
// Right pad
rightPadded := s + strings.Repeat(pad, width-len(s))