Skip to content

Instantly share code, notes, and snippets.

@caiguanhao
Created December 31, 2021 06:49
Show Gist options
  • Save caiguanhao/9c3e5633c2eb06b70d4a852f907be3ed to your computer and use it in GitHub Desktop.
Save caiguanhao/9c3e5633c2eb06b70d4a852f907be3ed to your computer and use it in GitHub Desktop.
func appendStringsIfMissing(slice []string, elements ...string) (out []string) {
out = slice
outer:
for _, element := range elements {
for _, e := range slice {
if e == element {
continue outer
}
}
out = append(out, element)
}
return
}
func appendStringIfMissing(slice []string, element string) []string {
for _, e := range slice {
if e == element {
return slice
}
}
return append(slice, element)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment