package main
import (
"fmt"
"strings"
)
func urlEncode(s string) string {
b := strings.Builder{}
b.Grow(len(s))
for _, c := range s {
if (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '/' {
b.WriteRune(c)
} else {
b.WriteString(fmt.Sprintf("%%%02x", c))
}
}
return b.String()
}
func main() {
s := "foo/bar+baz $qux!"
fmt.Println(urlEncode(s))
}
To embed this program on your website, copy the following code and paste it into your website's HTML: