Skip to content
A powerful language integrated query (LINQ) library for Go
Go
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
.travis.yml
LICENSE
README.md
aggregate.go
aggregate_test.go
benchmark_test.go
compare.go
compare_test.go
concat.go
concat_test.go
convert.go
convert_test.go
distinct.go
distinct_test.go
example_test.go
except.go
except_test.go
from.go
from_test.go
general_test.go
groupby.go
groupby_test.go
groupjoin.go
groupjoin_test.go
intersect.go
intersect_test.go
join.go
join_test.go
orderby.go
orderby_test.go
result.go
result_test.go
reverse.go
reverse_test.go
select.go
select_test.go
selectmany.go
selectmany_test.go
setup_test.go
skip.go
skip_test.go
take.go
take_test.go
union.go
union_test.go
where.go
where_test.go
zip.go
zip_test.go

README.md

go2linq GoDoc Build Status Coverage Status Go Report Card

A powerful language integrated query (LINQ) library for Go.

  • Safe for concurrent use
  • Complete lazy evaluation
  • Supports arrays, slices, maps, strings, channels and custom collections (collection needs to implement Iterable interface and element - Comparable interface)
  • Parallel LINQ (PLINQ) (comming soon)

⚠️ ⚠️ go2linq has been merged into the go-linq‘s master branch. Now it is available as the newest version of go-linq library (v2.0.0).

All further development will be conducted in the new location and this repository will remain only for compatibility reasons. Please, do not use it anymore!

Installation

$ go get github.com/kalaninja/go2linq

Quickstart

Usage is as easy as chaining methods like

From(slice) .Where(predicate) .Select(selector) .Union(data)

Just keep writing.

Example: Find all owners of cars manufactured from 2015

import . "github.com/kalaninja/go2linq"
	
type Car struct {
    id, year int
    owner, model string
}

owners := []string{}

From(cars).Where(func(c interface{}) bool {
	return c.(Car).year >= 2015
}).Select(func(c interface{}) interface{} {
	return c.(Car).owner
}).ToSlice(&owners)

Example: Find an author that has written the most books

import . "github.com/kalaninja/go2linq"
	
type Book struct {
	id      int
	title   string
	authors []string
}

author := From(books).SelectMany(func(b interface{}) Query {
		return From(b.(Book).authors)
	}).GroupBy(func(a interface{}) interface{} {
		return a
	}, func(a interface{}) interface{} {
		return a
	}).OrderByDescending(func(g interface{}) interface{} {
		return len(g.(Group).Group)
	}).Select(func(g interface{}) interface{} {
		return g.(Group).Key
	}).First()

More examples can be found in documentation

Performance

Due to lazy execution go2linq has better performance and allocates less memory in most cases. Benchmark comparing go2linq to go-linq is available in benchmark_test.go. Below is the result of this benchmark on my machine (MacBookPro8,1 Intel Core i5 2,4 GHz):

BenchmarkSelectWhereFirst-4       	 3000000	       561 ns/op	     224 B/op	      10 allocs/op
BenchmarkSelectWhereFirst_golinq-4	       2	 555810859 ns/op	120546360 B/op	 2000085 allocs/op
BenchmarkSum-4                    	      20	  73847428 ns/op	 8000289 B/op	 1000019 allocs/op
BenchmarkSum_golinq-4             	       5	 253731714 ns/op	69161392 B/op	 1000053 allocs/op
BenchmarkZipSkipTake-4            	 5000000	       351 ns/op	     192 B/op	       6 allocs/op
BenchmarkZipSkipTake_golinq-4     	       2	 672403213 ns/op	144520824 B/op	 3000075 allocs/op
You can’t perform that action at this time.