Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@techslides
Created February 13, 2014 16:38
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save techslides/8978661 to your computer and use it in GitHub Desktop.
Save techslides/8978661 to your computer and use it in GitHub Desktop.
Generating Pretty Urls for SEO in GoLang using string and regex packages
type Post struct {
// db tag lets you specify the column name if it differs from the struct field
Id int64 `db:"post_id"`
Created int64
Title string `form:"Title" binding:"required"`
Body string `form:"Body"`
UserId int64 `form:"UserId"`
Url string
}
//function to create a new Post object
func newPost(title string, body string, user int64) Post {
//let's make pretty urls from title
reg, err := regexp.Compile("[^A-Za-z0-9]+")
if err != nil {
log.Fatal(err)
}
prettyurl := reg.ReplaceAllString(title, "-")
prettyurl = strings.ToLower(strings.Trim(prettyurl, "-"))
return Post{
Created: time.Now().Unix(),
Title: title,
Body: body,
UserId: user,
Url: prettyurl,
}
}
@techslides
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment