Introducing LINQed-Go: A Modern Take on LINQ for Go
When working on a project using Go's net/http
standard library and GORM, I found myself bogged down by the repetitive task of filtering entities. Writing loops, defining types, and managing filters felt like an unnecessary chore that interrupted the flow of development. Inspired by the elegance and efficiency of C#'s LINQ, I decided to create LINQed-Go, a Go package designed to bring the simplicity of LINQ-style data manipulation to the Go ecosystem.
Why linqed-go?
If you've ever worked with Go, you know that dealing with slices, maps, and channels can sometimes require verbose looping logic. With LINQed-Go, you can filter, transform, and query your collections with clear, declarative operations. No more boilerplate loops — just simple, expressive methods.
Key Features
- Intuitive Syntax: Write expressive, chainable operations like
Where
,Select
,OrderBy
,Skip
, andTake
. - Support for Multiple Collections: Works with slices, maps, and channels.
- Deferred Execution: Operations like
Where
andSelect
are evaluated only when needed, ensuring optimal performance. - Comprehensive LINQ Methods: Supports essential operations such as
First
,Any
,All
,Count
,Distinct
,Sum
,Min
,Max
,Concat
,Join
, andAggregate
.
Example Usage
Here’s a simple example of how LINQed-Go can make your Go code cleaner and more readable.
linqed-go/main.go
package main
import (
"fmt"
"github.com/onursedef/linqed-go"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// Filter even numbers and multiply them by 2
query := linqed.From(numbers).
Where(func(n int) bool { return n%2 == 0 })
result := linqed.Select(query, func(i int) int {
return i
}).Iterate()
fmt.Println(result) // Output: [4, 8, 12, 16, 20]
}
With the power of linqed-go, the above operation is clean, expressive, and avoids the usual boilerplate code.
LINQed-Go vs. Traditional Go Approach
To better understand the power of LINQed-Go, let's see how you would write the same logic in traditional Go.
Using LINQed-Go
linqed-go/main.go
// Create a queryable collection
query := linqed_go.From(people).Where(func(p Person) bool {
return p.Age >= 30
})
// Example of Where and Select methods
result := linqed_go.Select(query, func(p Person) string {
return p.Name
}).Iterate()
Traditional Go
linqed-go/main.go
var result []int
for _, n := range numbers {
if n%2 == 0 {
result = append(result, n*2)
}
}
As you can see, LINQed-Go eliminates the need for manual iteration, condition checks, and append
logic, making the code cleaner and more readable.
Built for Go Developers
Unlike other LINQ-inspired libraries, LINQed-Go is designed with Go's idioms in mind. It doesn't try to force Go into a C#-like mold, but rather adopts the best aspects of LINQ while maintaining the performance and simplicity that Go developers expect.
Get Started
- Install the package:
bash
go get -u github.com/onursedef/linqed-go
Check out the documentation: Explore the complete documentation on pkg.go.dev to see all available operations and examples.
Start coding: Replace your manual loops with expressive LINQ-style operations.
Final Thoughts
Whether you’re working on APIs, data pipelines, or complex queries with GORM, LINQed-Go can simplify your workflow. If you’ve ever admired the elegance of LINQ in C#, now you can bring that same power to your Go projects.
Give it a try, and feel free to share your feedback or contribute to its development. Let’s make Go’s data manipulation as elegant as its concurrency model.
Check it out here: linqed-go on github.com
Happy coding! 🚀