1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package util
- import (
- "errors"
- "reflect"
- )
- func AppendIfNotExists(slice []string, element string) []string {
- for _, item := range slice {
- if item == element {
- return slice
- }
- }
- return append(slice, element)
- }
- func MergeSlice(slice1 []string, slice2 []string) []string {
-
- for _, element := range slice2 {
- found := false
- for _, item := range slice1 {
- if element == item {
- found = true
- break
- }
- }
- if !found {
- slice1 = append(slice1, element)
- }
- }
- return slice1
- }
- func ContainsElement(slice interface{}, element interface{}) (bool, error) {
- sliceValue := reflect.ValueOf(slice)
- if sliceValue.Kind() != reflect.Slice {
- return false, errors.New("没有输入切片。")
- }
- for i := 0; i < sliceValue.Len(); i++ {
- currentElement := sliceValue.Index(i).Interface()
- if reflect.DeepEqual(currentElement, element) {
- return true, nil
- }
- }
- return false, nil
- }
|