Sling Academy
Home/Golang/How to generate slug from title in Go

How to generate slug from title in Go

Last updated: November 28, 2024

When building web applications in Go, you often need to generate URL-friendly slugs from titles. Slugs are typically used in the URL structure of websites because they are more readable and SEO-friendly.

What is a Slug?

A slug is a part of a URL that identifies a particular page on a website in a form that is easy to read for both users and search engines. Generally, slugs are created by converting a string to lowercase, replacing spaces with hyphens, and removing any special characters.

Steps to Generate a Slug

To generate a slug in Go, we follow these steps:

  1. Convert the string to lowercase.
  2. Replace spaces with hyphens.
  3. Remove any special characters.

Example: Generating a Slug in Go

Here is a step-by-step guide with code snippets to generate a slug from a title in Go:

Step 1: Create a New Go Module

First, initialize a new Go module:


go mod init example.com/slug-generator

Step 2: Write the Slug Generator Function

Here is an example function to generate a slug:

package main

import (
	"fmt"
	"regexp"
	"strings"
)

// Slugify converts a text string into a slug.
func Slugify(s string) string {
	// Convert to lowercase
	result := strings.ToLower(s)

	// Replace spaces with hyphens
	result = strings.ReplaceAll(result, " ", "-")

	// Remove non-alphanumeric characters except for hyphens
	reg, err := regexp.Compile("[^a-z0-9-]+")
	if err != nil {
		fmt.Println(err)
		return ""
	}
	result = reg.ReplaceAllString(result, "")

	// Return the slug
	return result
}

func main() {
	title := "Hello World: A Beginner's Guide!"
	slug := Slugify(title)
	fmt.Println(slug) // Output: hello-world-a-beginners-guide
}

Explanation:

  • We first convert the input string to lowercase using strings.ToLower.
  • We replace spaces with hyphens using strings.ReplaceAll.
  • We use regular expressions to remove any characters that are not alphanumeric or hyphens. regexp.Compile("[^a-z0-9-]+") creates a regular expression that matches anything outside the allowed character set.

Conclusion

This Go-based solution provides a simple and effective way to generate slugs from titles. You can enhance this basic example by considering different character sets, trimming hyphens, or other custom rules as per your application's needs.

Next Article: How to remove HTML tags in a string in Go

Previous Article: How to remove consecutive whitespace in a string in Go

Series: Working with Strings in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant
  • Fixing Go error: syntax error: unexpected X, expecting Y