gitlab.com/evatix-go/core@v1.3.55/coredata/stringslice/SafeIndexes.go (about)

     1  package stringslice
     2  
     3  import "gitlab.com/evatix-go/core/constants"
     4  
     5  // SafeIndexes get safe indexes slice based on indexes given
     6  //
     7  // Reference : https://play.golang.org/p/uKLpK8go0lh
     8  func SafeIndexes(slice []string, indexes ...int) []string {
     9  	lastIndex := len(slice) - 1
    10  	requestLength := len(indexes)
    11  	responseSlice := MakeLen(requestLength)
    12  	if lastIndex < constants.Zero {
    13  		return responseSlice
    14  	}
    15  
    16  	for i := 0; i < requestLength; i++ {
    17  		index := indexes[i]
    18  		if index > lastIndex || index < 0 {
    19  			continue
    20  		}
    21  
    22  		responseSlice[i] = slice[index]
    23  	}
    24  
    25  	return responseSlice
    26  }