Created
December 31, 2021 06:49
-
-
Save caiguanhao/9c3e5633c2eb06b70d4a852f907be3ed to your computer and use it in GitHub Desktop.
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
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