Skip to content

Instantly share code, notes, and snippets.

@walm
Created February 11, 2015 17:52
Show Gist options
  • Save walm/1ce1d7ca728cd4bfccd4 to your computer and use it in GitHub Desktop.
Save walm/1ce1d7ca728cd4bfccd4 to your computer and use it in GitHub Desktop.
Unique values from slice of string
// in Go Play: http://play.golang.org/p/G5e-E8fe7h
package main
import "fmt"
func UniqStrs(slice []string) []string {
m := map[string]bool{}
for _, v := range slice {
m[v] = true
}
r := make([]string, len(m))
i := 0
for v := range m {
r[i] = v
i++
}
return r
}
func main() {
v := []string{
"hello",
"kalle",
"test",
"hello",
"Kalle",
"wow",
"hello",
}
fmt.Println("Values %v", UniqStrs(v))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment