Golang : Create unique title slugs example
A simple example on how to create title slug from a string and also demonstrate what you can do in case the title slug is not unique after querying the database. In this example, we append the number from 1 to 10 in case there's a conflict of title slugs in the database. The range of numbers from 1 to 10 should be sufficient. Feel free to increase to 100 to ensure there are more rooms to generate unique title slugs.
Here you go!
package main
import (
"fmt"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"regexp"
"strconv"
"strings"
"unicode"
)
func isMn(r rune) bool {
return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
}
func stringToSlug(s string) (string, error) {
str := []byte(strings.ToLower(s))
// convert all spaces to dash
regE := regexp.MustCompile("[[:space:]]")
str = regE.ReplaceAll(str, []byte("-"))
// remove all blanks such as tab
regE = regexp.MustCompile("[[:blank:]]")
str = regE.ReplaceAll(str, []byte(""))
// remove all punctuations with the exception of dash
regE = regexp.MustCompile("[!/:-@[-`{-~]")
str = regE.ReplaceAll(str, []byte(""))
regE = regexp.MustCompile("/[^\x20-\x7F]/")
str = regE.ReplaceAll(str, []byte(""))
regE = regexp.MustCompile("`&(amp;)?#?[a-z0-9]+;`i")
str = regE.ReplaceAll(str, []byte("-"))
regE = regexp.MustCompile("`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i")
str = regE.ReplaceAll(str, []byte("\\1"))
regE = regexp.MustCompile("`[^a-z0-9]`i")
str = regE.ReplaceAll(str, []byte("-"))
regE = regexp.MustCompile("`[-]+`")
str = regE.ReplaceAll(str, []byte("-"))
strReplaced := strings.Replace(string(str), "&", "", -1)
strReplaced = strings.Replace(strReplaced, `"`, "", -1)
strReplaced = strings.Replace(strReplaced, "&", "-", -1)
strReplaced = strings.Replace(strReplaced, "--", "-", -1)
if strings.HasPrefix(strReplaced, "-") || strings.HasPrefix(strReplaced, "--") {
strReplaced = strings.TrimPrefix(strReplaced, "-")
strReplaced = strings.TrimPrefix(strReplaced, "--")
}
if strings.HasSuffix(strReplaced, "-") || strings.HasSuffix(strReplaced, "--") {
strReplaced = strings.TrimSuffix(strReplaced, "-")
strReplaced = strings.TrimSuffix(strReplaced, "--")
}
// normalize unicode strings and remove all diacritical/accents marks
// see https://www.socketloop.com/tutorials/golang-normalize-unicode-strings-for-comparison-purpose
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
slug, _, err := transform.String(t, strReplaced)
if err != nil {
return "", err
}
return strings.TrimSpace(slug), nil
}
func main() {
testStr := "?This is a test-title with question mark ? and& \" exclaimation mark !"
testSlug, err := stringToSlug(testStr)
if err != nil {
panic(err)
}
fmt.Printf("%v to:\n %v\n", testStr, testSlug)
// in case you encounter a situation where the slug title is not unique after you query the database.
// just append a number the string with a number to make it unique
// NOTE : You will have to make a function of your own here to query your database
newSlug := ""
for i := 1; i <= 10; i++ {
newSlug = testSlug + "-" + strconv.Itoa(i)
// here is where you want to query your database
// if newSlug is unique, break
// let's pretend that number 5 is unique after querying
// the database 5 times.
if i == 5 {
break
}
}
// our unique slug title
fmt.Println(newSlug)
}
Output:
?This is a test-title with question mark ? and& " exclaimation mark ! to:
this-is-a-test-title-with-question-mark-and-exclaimation-mark
this-is-a-test-title-with-question-mark-and-exclaimation-mark-5
NOTE: Before inserting the title slugs into your database, it would be advisable to trim the title first.
References:
https://www.socketloop.com/tutorials/golang-format-strings-to-seo-friendly-url-example
https://www.socketloop.com/tutorials/golang-increment-string-example
See also : Golang : Format strings to SEO friendly URL example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+8.4k Golang : Progress bar with ∎ character
+4.2k Linux/MacOSX : Search and delete files by extension
+5.1k Unix/Linux : How to archive and compress entire directory ?
+5.7k Golang : Extract unicode string from another unicode string example
+10.2k Golang : Meaning of omitempty in struct's field tag
+7.3k Golang : How to stop user from directly running an executable file?
+7.6k Golang : Regular Expression find string example
+17.9k Golang : Get command line arguments
+8.3k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+25k Golang : Convert long hexadecimal with strconv.ParseUint example
+8k Golang : Metaprogramming example of wrapping a function
+4.9k Golang : Convert lines of string into list for delete and insert operation