linq

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2019 License: MIT Imports: 4 Imported by: 0

README

Implementation of LINQ queries on Golang

Go Report Card Codacy Badge Build Status Coverage Status

Goal

Simplify operations over slices and study of working of reflection

Queries

Supported queries:

  • Where

Where

package main

import (
	"fmt"

	"github.com/saromanov/go-linq"
)

func main() {
	l, err := linq.New([]int{1, 4, 5, 8, 9})
	if err != nil {
		panic(err)
	}
	fmt.Println(l.Where(func(x int) bool {
		if x > 4 {
			return true
		}
		return false
	}).Result().([]int)
}

[5 8 9]

In this example: First, create a Linq object, then apply of the Where method with filter. Filter function should be go func(x int) bool{}. At the last step, showing of response with Result method

Select

Select method to modify the elements on array

package main

import (
	"fmt"

	"github.com/saromanov/go-linq"
)

type Data struct {
	Key   int
	Value int
}

func main() {
	l, err := linq.New([]Data{Data{
		Key:   1,
		Value: 2,
	},
		Data{
			Key:   3,
			Value: 5,
		},
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(l.Select(func(x Data) int {
		return x.Key
	}).Result().([]int))

}```

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Enumerable

type Enumerable interface {
	GetList() []interface{}
	Compare(interface{}) int
}

Enumerable defines main interface for collections

type Linq

type Linq struct {
	// contains filtered or unexported fields
}

Linq defines main structure on collection

func New

func New(data interface{}) (*Linq, error)

New creates init of the linq

func (*Linq) First

func (l *Linq) First(f interface{}) *Linq

First provides getting of the first element from collection

func (*Linq) Last

func (l *Linq) Last(f interface{}) *Linq

Last provides getting of the last element from collection

func (*Linq) OrderBy

func (l *Linq) OrderBy(f interface{}) *Linq

OrderBy provides sorting of elements to increasing

func (*Linq) Result

func (l *Linq) Result() interface{}

Result returns response after operations

func (*Linq) Select

func (l *Linq) Select(f interface{}) *Linq

Select defines a method for mapping of the representation of collection on the new form

func (*Linq) ThenBy

func (l *Linq) ThenBy(f interface{}) *Linq

ThenBy provides sorting by the key It should have "sorted": true after call of OrderBy

func (*Linq) Where

func (l *Linq) Where(f interface{}) *Linq

Where provides filtering on collection

func (*Linq) Zip

func (l *Linq) Zip(coll interface{}, f interface{}) *Linq

Zip provides applying a specified function to the corresponding elements of two sequences, producing a sequence of the results.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL