Created
June 20, 2014 09:09
-
-
Save hariharan-uno/030b1568d6807d1d1ae5 to your computer and use it in GitHub Desktop.
Auto incrementing slugs
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
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