about me
Gospel for developers, go also supports linq
Cheating collection
Go is very uncomfortable when performing collection operations. At first, I was really unable to complain, and I couldn't find a good third-party library, so I could only write repetitive code every time. Give a chestnut
类 学生{
姓名
年龄
性别
}
1. Now there is an array of 10 students. If I want to count all the people who are older than 20 years old, then I need
1. Traverse
Two, custom conditions
Third, add the append array again.
2. Then I want to count all the students whose gender is male, and I have to repeat the above steps again.
You say cheating is not cheating, that is really cheating!
Tucao
The point is that 95% of the code is the same, but the percentage is slightly different. For a qualified programmer, I can't stand this situation.
Solution
Seek help from God in the group
Find effective information in major forums and blogs
Finally, under the unremitting efforts of the old man, a library was discovered. It is go-linq . Using it can solve most of my needs for collections, make programming easier, and make work more efficient.
What is Linq?
LINQ (pronounced: Link) is a language-level integrated query (Language INtegrated Query)
• LINQ is a programming model for data access, so that the .NET language can directly support data query
Linq is a cool flying syntactic sugar for C# programming. Everyone who has used it is impressed by its advanced features and partial natural semantics.
Then the problem is coming
So the question is, is there anything similar to go? The answer is yes, this time it is the library Go-linq we are about to talk about . You will know what he does by this name. Not much to say, just open Lu.
Start using Go-Linq
Introduction to Go-Linq
A powerful language integrated query (LINQ) library for Go.
No dependencies!
Use iterator mode to complete delayed evaluation
Safe for concurrent use
Support generic functions, make your code cleaner, and no type assertions
Support arrays, slices, maps, strings, channels and custom collections
use
go get gopkg.in/ahmetb/go-linq.v3
import . "gopkg.in/ahmetb/go-linq.v3"
Import. means to use the library methods directly, without using prefixes. Of course you can also add, the official wording is like this.
Case study
Define an employee class
type Employee struct {
Name string
Age int
Sex int // 0 男 1 女
WorkYear int //工龄
}
Create different lists
func initEmployeeData() []Employee {
list := make([]Employee, 0)
for i := 0; i < 10; i++ {
list = append(list, Employee{
Name: "张" + strconv.Itoa(i%4),
Age: 10 + i,
Sex: i % 2,
WorkYear: 1 + i%3,
})
}
return list
}
func initSameEployeeData() []Employee {
list := make([]Employee, 0)
for i := 0; i < 10; i++ {
list = append(list, Employee{
Name: "张一",
Age: 10,
Sex: i % 2,
WorkYear: 1,
})
}
return list
}
Small test sledge-distinct removal start
func distinct() {
var manEmpRows []Employee
rows := initSameEployeeData()
fmt.Println("===性别是男的所有员工列表去重===")
From(rows).Distinct().ToSlice(&manEmpRows)
fmt.Println(manEmpRows)
}
===Remove the list of all employees whose gender is male ===
[{张一10 0 1} {张一10 1 1}]
The result is very nice, the cumbersome steps we needed, a linq can be solved, is it not Diao!
Find him in the public thousands of Baidu-where filtering
//where 过滤条件
var manEmpRows []Employee
fmt.Println("===过滤性别是男的员工===")
From(rows).WhereT(func(e Employee) bool {
return e.Sex == 0
}).ToSlice(&manEmpRows)
fmt.Println(manEmpRows)
Won the top three-take+sort
//Take 选取从头开始的几个元素
fmt.Println("===过滤性别是男的员工,只选择前俩个===")
From(rows).WhereT(func(e Employee) bool {
return e.Sex == 0
}).Take(2).ToSlice(&manEmpRows)
fmt.Println(manEmpRows)
Sort. Single field sorting, multi-field combination sorting.
//where过滤+排序
fmt.Println("===过滤性别是女的员工,且按照工龄降序排序===")
From(rows).WhereT(func(e Employee) bool {
return e.Sex == 1
}).OrderByDescendingT(func(e Employee) int {
return e.WorkYear
}).ToSlice(&manEmpRows)
fmt.Printf("%+v\n", manEmpRows)
//where 过滤+双重排序
fmt.Println("===过滤性别是女的员工,且按照工龄降序排序,再按照年龄升序排序===")
From(rows).WhereT(func(e Employee) bool {
return e.Sex == 1
}).OrderByDescendingT(func(e Employee) int {
return e.WorkYear
}).ThenByT(func(e Employee) int {
return e.Age
}).ToSlice(&manEmpRows)
Take one scoop for three thousand weak water-Select
//只获取元素中的某些字段,list输出
var outputRows []string
fmt.Println("===只获取元素中的某些字段,list输出===")
From(rows).SelectT(func(e Employee) string {
return e.Name
}).ToSlice(&outputRows)
fmt.Println(outputRows)
Bee synth poly-aggregation
//聚合函数
query := From(rows).SelectT(func(e Employee) int {
return e.Age
})
fmt.Println(query.Average())
fmt.Println(query.Max())
fmt.Println(query.Min())
fmt.Println(query.Count())[]
other
//获取结构体数组首个元素或者末个
firstItem := From(rows).First()
fmt.Println(firstItem)
lastItem := From(rows).Last()
fmt.Println(lastItem)
to sum up
Through the introduction, I don't know if you have a simple understanding of go-linq and a general understanding of the use of Linq. If there is, then refer to the coding and touch it by yourself to strengthen the impression.
For other features, please check the official description for yourself, and there are more interesting Linq syntax sugars waiting for you to explore.
Reference
At last
Recommended reading
New open source has emerged after Redis tool charges
Star's highest engineer skill map on GitHub
The most prone word for Chinese programmers to send wrong words
Recommended!!! Markdown icon index website )
This concludes this article, I hope it helps you