charleslxh / go-funk

A modern Go utility library which provides helpers (map, find, contains, filter, ...)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

go-funk

Build Status

GoDoc

Go report

go-funk is a modern Go library based on reflect.

Generic helpers rely on reflect, be careful this code runs exclusively on runtime so you must have a good test suite.

These helpers have started as an experiment to learn reflect. It may look like lodash in some aspects but it will have its own roadmap. lodash is an awesome library with a lot of work behind it, all features included in go-funk come from internal use cases.

You can also find typesafe implementation in the godoc.

Why this name?

Long story, short answer because func is a reserved word in Go, I wanted something similar.

Initially this project was named fn I don't need to explain why that was a bad idea for french speakers :)

Let's funk!

image

<3

Installation

Usage

These examples will be based on the following data model:

With fixtures:

You can import go-funk using a basic statement:

funk.Contains

Returns true if an element is present in a iteratee (slice, map, string).

One frustrating thing in Go is to implement contains methods for each type, for example:

this can be replaced by funk.Contains:

see also, typesafe implementations: ContainsInt, ContainsInt64, ContainsFloat32, ContainsFloat64, ContainsString

funk.IndexOf

Gets the index at which the first occurrence of a value is found in an array or return -1 if the value cannot be found.

see also, typesafe implementations: IndexOfInt, IndexOfInt64, IndexOfFloat32, IndexOfFloat64, IndexOfString

funk.LastIndexOf

Gets the index at which the last occurrence of a value is found in an array or return -1 if the value cannot be found.

see also, typesafe implementations: LastIndexOfInt, LastIndexOfInt64, LastIndexOfFloat32, LastIndexOfFloat64, LastIndexOfString

funk.ToMap

Transforms a slice of structs to a map based on a pivot field.

funk.Filter

Filters a slice based on a predicate.

see also, typesafe implementations: FilterInt, FilterInt64, FilterFloat32, FilterFloat64, FilterString

funk.Find

Finds an element in a slice based on a predicate.

see also, typesafe implementations: FindInt, FindInt64, FindFloat32, FindFloat64, FindString

funk.Map

Manipulates an iteratee (map, slice) and transforms it to another type:

  • map -> slice
  • map -> map
  • slice -> map
  • slice -> slice

funk.Get

Retrieves the value at path of struct(s).

funk.Get also handles nil values:

funk.Keys

Creates an array of the own enumerable map keys or struct field names.

funk.Values

Creates an array of the own enumerable map values or struct field values.

funk.ForEach

Range over an iteratee (map, slice).

funk.ForEachRight ............

Range over an iteratee (map, slice) from the right.

funk.Chunk

Creates an array of elements split into groups with the length of the size. If array can't be split evenly, the final chunk will be the remaining element.

funk.FlattenDeep

Recursively flattens an array.

funk.Uniq

Creates an array with unique values.

see also, typesafe implementations: UniqInt, UniqInt64, UniqFloat32, UniqFloat64, UniqString

funk.Drop

Creates an array/slice with n elements dropped from the beginning.

see also, typesafe implementations: DropInt, DropInt32, DropInt64, DropFloat32, DropFloat64, DropString

funk.Initial

Gets all but the last element of array.

funk.Tail

Gets all but the first element of array.

funk.Shuffle

Creates an array of shuffled values.

see also, typesafe implementations: ShuffleInt, ShuffleInt64, ShuffleFloat32, ShuffleFloat64, ShuffleString

funk.Sum

Computes the sum of the values in an array.

see also, typesafe implementations: SumInt, SumInt64, SumFloat32, SumFloat64

funk.Reverse

Transforms an array such that the first element will become the last, the second element will become the second to last, etc.

see also, typesafe implementations: ReverseInt, ReverseInt64, ReverseFloat32, ReverseFloat64, ReverseString, ReverseStrings

funk.SliceOf

Returns a slice based on an element.

funk.RandomInt

Generates a random int, based on a min and max values.

funk.RandomString

Generates a random string with a fixed length.

funk.Shard

Generates a sharded string with a fixed length and depth.

Performance

go-funk currently has an open issue about performance, don't hesitate to participate in the discussion to enhance the generic helpers implementations.

Let's stop beating around the bush, a typesafe implementation in pure Go of funk.Contains, let's say for example:

will always outperform an implementation based on reflect in terms of speed and allocs because of how it's implemented in the language.

If you want a similarity, gorm will always be slower than sqlx (which is very low level btw) and will use more allocs.

You must not think generic helpers of go-funk as a replacement when you are dealing with performance in your codebase, you should use typesafe implementations instead.

Contributing

Don't hesitate ;)

Authors

  • Florent Messa
  • Gilles Fabio
  • Alexey Pokhozhaev
  • Alexandre Nicolaie

About

A modern Go utility library which provides helpers (map, find, contains, filter, ...)

License:MIT License


Languages

Language:Go 100.0%Language:Makefile 0.0%