github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/container/slice/helper.go (about)

     1  package slice
     2  
     3  import (
     4  	"strconv"
     5  )
     6  
     7  // Contains Check item in slice string type
     8  func Contains(slice []string, item string) bool {
     9  	set := make(map[string]struct{}, len(slice))
    10  	for _, s := range slice {
    11  		set[s] = struct{}{}
    12  	}
    13  
    14  	_, ok := set[item]
    15  	return ok
    16  }
    17  
    18  // ContainsRune Check item in map rune type
    19  func ContainsRune(set map[rune]struct{}, item rune) bool {
    20  	_, ok := set[item]
    21  	return ok
    22  }
    23  
    24  // ContainsValue check if value exists in slice, no matter its type
    25  func ContainsValue(slice []interface{}, value interface{}) bool {
    26  	for _, v := range slice {
    27  		if v == value {
    28  			return true
    29  		}
    30  	}
    31  	return false
    32  }
    33  
    34  // IntToString Convert slice int to slice string
    35  func IntToString(intSl []int) (str []string) {
    36  	for i := range intSl {
    37  		number := intSl[i]
    38  		text := strconv.Itoa(number)
    39  		str = append(str, text)
    40  	}
    41  	return str
    42  }