Skip to content

Instantly share code, notes, and snippets.

@hariharan-uno
Created June 20, 2014 09:09
Show Gist options
  • Save hariharan-uno/030b1568d6807d1d1ae5 to your computer and use it in GitHub Desktop.
Save hariharan-uno/030b1568d6807d1d1ae5 to your computer and use it in GitHub Desktop.
Auto incrementing slugs
package main
import (
"fmt"
"strconv"
"github.com/extemporalgenome/slug"
)
type person struct {
name string
slug string
}
var pool [5]*person
var slugs map[string]string
func main() {
slugs = make(map[string]string)
pool[0] = &person{name: "Hari haran"}
pool[1] = &person{name: "Sam Flynn"}
pool[2] = &person{name: "Hari haran"}
pool[3] = &person{name: "Hari haran"}
pool[4] = &person{name: "Sam Flynn"}
for _, item := range pool {
slugify(item)
fmt.Println(item)
}
}
func slugify(p *person) {
base := slug.SlugAscii(p.name)
temp := base
i := 1
for {
if _, ok := slugs[temp]; ok {
temp = base + "-" + strconv.Itoa(i)
i++
} else {
p.slug = temp
slugs[temp] = p.name
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment