Skip to content

Instantly share code, notes, and snippets.

@mchampaneri
Created December 20, 2018 05:51
Show Gist options
  • Save mchampaneri/679a64bb8340869c9a2841dbd72e9849 to your computer and use it in GitHub Desktop.
Save mchampaneri/679a64bb8340869c9a2841dbd72e9849 to your computer and use it in GitHub Desktop.
string padding helper functions
package main
import "fmt"
func PadRight(str, pad string, lenght int) string {
for {
str += pad
if len(str) > lenght {
return str[0:lenght]
}
}
}
func PadLeft(str, pad string, lenght int) string {
for {
str = pad + str
if len(str) > lenght {
return str[1:lenght+1]
}
}
}
func main() {
str := fmt.Sprintf("%d",123)
fmt.Println(PadRight(str, "x", 5)) // expects abcxx
fmt.Println(PadLeft(str, "x", 5)) // expects xxabc
fmt.Println(PadRight(str, "xyz", 5)) // expects abcxy
fmt.Println(PadLeft(str, "xyz", 5)) // expects xyzab
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment