Skip to content

C# System.Linq Enumerable methods and System.Collections.Generic List realization by go generic.

License

Notifications You must be signed in to change notification settings

STRockefeller/go-linq

Repository files navigation

Go-Linq

licensecode sizeopen issuesclosed issues go versionlatest version

GitHub Super-LinterGo Report CardCoverage Status

C# System.Linq Enumerable methods and System.Collections.Generic List realization by go generic.

Notice

golang version must be greater than v1.18

for previous go versions (<1.18), you can try this one.

Usage

install package

go get github.com/STRockefeller/go-linq

import package

import "github.com/STRockefeller/go-linq"

enjoy

func linqTest() {
 type user struct {
  name string
  age  int
 }
 users := linq.New([]user{})
 users.Add(user{
  name: "Rockefeller",
  age:  27,
 })
 users.Prepend(user{
  name: "newUser",
  age:  18,
 })
 adultsCount := users.Where(func(u user) bool { return u.age >= 18 }).Count(func(u user) bool {return true})
 fmt.Println("there are ",adultsCount,"adults in users.")
}

See pkg.go.dev document for details

Benchmark

compare with another linq package.

package main

import (
 "fmt"
 "testing"

 STRLinq "github.com/STRockefeller/go-linq"
 AHMLinq "github.com/ahmetb/go-linq/v3"
)

var testStrings = []string{"Oh,", "mister", "ocean", "fish", "!"}
var testIntegers = []int{7, 5, 3, 9, 5, 1, 7, 4, 1, 0, 3, 6, 9}

func BenchmarkSTRockefeller_linq(b *testing.B) {
 for i := 0; i < b.N; i++ {
  STRLinq.New(testStrings).Where(func(s string) bool { return len(s) >= 3 }).Skip(1).Contains("mister")

  mySlice := STRLinq.New(testIntegers).Distinct().Where(func(i int) bool { return i > 3 }).ToSlice()

  if false {
   fmt.Print(mySlice)
  }
 }
}

func BenchmarkAhmetb_linq(b *testing.B) {
 for i := 0; i < b.N; i++ {
  AHMLinq.From(testStrings).Where(func(i interface{}) bool { return len(i.(string)) >= 3 }).Skip(1).Contains("mister")

  var mySlice []int
  AHMLinq.From(testIntegers).Distinct().Where(func(i interface{}) bool { return i.(int) > 3 }).ToSlice(&mySlice)

  if false {
   fmt.Print(mySlice)
  }
 }
}

result

goos: windows
goarch: amd64
pkg: test
cpu: Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
BenchmarkSTRockefeller_linq-8     2529393        467.3 ns/op      400 B/op       12 allocs/op
BenchmarkAhmetb_linq-8             521704       2121 ns/op     1080 B/op       45 allocs/op
PASS
coverage: [no statements]
ok   test 3.004s

About

C# System.Linq Enumerable methods and System.Collections.Generic List realization by go generic.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages