TOP ▲
itcore TOP
> TIPS
> go_contains.php
タグ:go go共通関数 slice要素 存在チェック、削除 | itcore 2021年
// sliceに要素が存在しているか(文字型)
func contains(slice []string, element string) bool {
for _, value := range slice {
if element == value {
return true
}
}
return false
}
// sliceの順番を維持して要素を削除する。(文字型)
func remove(slice []string, element string) []string {
result := []string{}
for _, value := range slice {
if value != element {
result = append(result, value)
}
}
return result
}