Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/franpog859/index

📦📆 A little Go package with high level generic like functions for simple slices
https://github.com/franpog859/index

generic generics golang index interface package reflect slice stackoverflow

Last synced: about 1 month ago
JSON representation

📦📆 A little Go package with high level generic like functions for simple slices

Awesome Lists containing this project

README

        



[![Documentation](https://godoc.org/github.com/franpog859/index?status.svg)](http://godoc.org/github.com/franpog859/index)
[![Go Report Card](https://goreportcard.com/badge/github.com/franpog859/index)](https://goreportcard.com/report/github.com/franpog859/index)
[![CircleCI](https://circleci.com/gh/franpog859/index.svg?style=shield)](https://circleci.com/gh/franpog859/index)

## Description

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](https://golang.org/pkg/reflect/) package and [this](https://stackoverflow.com/questions/12753805/type-converting-slices-of-interfaces-in-go) idea turned out to be a very good solution.

## Usage

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:

```go
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](https://github.com/franpog859/index/blob/master/USAGE.md) file.

## Contribution

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 :wink: See also [CONTRIBUTING.md](https://github.com/franpog859/index/blob/master/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!