LINQ

Go LINQ is a query package for Go. Essentially it has ability to apply
queries to slices and collections using SQL-like methods.

Installation

As any other go package we should execute go get command:

$ go get ahmetalpbalkan.github.io/go-linq

Usage

The package consists two query structs that have a set of functions for querying,
projection, grouping, filtering, sorting, aggregation and many more which will
explore in detail. Queries are processed synchronously and asynchronously as well. It
does not evaluate the data lazily. No deferred execution due to lack of enumeration
abstraction. The package works only with slices.

The package itself uses reflection to work any slice or collection of data. It
declares linq.T interface that used by most of the package functions. In order to work
with a concrete type, it must be casted:

var value linq.T
obj := value.(yourType)

Examples

The example are pretty similar to my previous blog post where I am using [Gen]().
The difference between both libraries is that Gen relies on code generation
while LINQ works by using reflection. I haven’t done any performance comparisions
to evaluate how slow LINQ is.

Grouping a slice is processed by GroupBy function which accepts as argument
two selector functions. The first clojure selects the group key, while the second
returns the object for that key. The result is again a map[T]T.

The package provides us with a join function that correlates the elements of
collections based on their equality. The first two clojure functions extract the
key for every item in each slice. The third functions extracts the result that
we want.

In the example below the inner join is between companies and countries slice
based on company.Counutry and country.Name properties. Objects that have the
same property value are correlated.