Created
February 13, 2014 16:38
-
-
Save techslides/8978661 to your computer and use it in GitHub Desktop.
Generating Pretty Urls for SEO in GoLang using string and regex packages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Learn more about this code here: http://techslides.com/martini-app-with-ajax-json-and-user-sessions/