Index is a little Go package. It provides high level generic like functions for simple slices.
It allows you to check on which indexes your item is positioned in the slice. Index also allows you to check if your item exist in the slice or how many of them are there. It works with every type of simple slices! There is a common Go problem to pass the slice with unknown type to the function. It is solved here with the reflect package and this idea turned out to be a very good solution.
To get this package and use it just type in your terminal go get github.com/franpog859/index
. After that you just can simply import the package in your code import "github.com/franpog859/index"
and use it as follows:
package main
import (
"fmt"
"github.com/franpog859/index"
)
func main() {
slice := []int{1, 2, 3, 1}
item := 1
indexes, err := index.GetAll(slice, item)
if err != nil {
fmt.Println(err)
}
fmt.Println(indexes)
}
Output:
[0 3]
Remember not to pass some complex slices like multidimensional slices or slices of maps. This package just does not deal with such complexity so remember to check the err
value!
If you want to see examples of usage other functions go to USAGE.md file.
If you want to make this package better just fork this repository and prepare your pull request! Remember to keep the code clean and to test your implementation 😉 See also CONTRIBUTING.md file for more informations about preparing your pull request and the workflow.
To test this package run in your terminal:
git clone https://github.com/franpog859/index.git
cd index
go test -cover ./...
If you see some bug or bad habit feel free to tell me!