Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Go-Linq

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to use Go-Linq". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Go-Linq.

A deceptive collection

There was something uncomfortable with go when doing collection operations. At first, I really couldn't complain, and I couldn't find a good third-party library, so I had to repeat the code every time. Take a chestnut.

Class students {name, age, sex}

1. There is now an array of 10 students. If I want to count all the people over the age of 20, then I need

First, traversing

II. Custom conditions

Third, add it to the append array.

2. Then I will count all the students whose gender is male, and I will repeat the above steps.

If you say it's not a trick, it's a real trick!

Complaining

The point is that 95% of the code is the same, but that percentage is a little different. For a qualified programmer, I absolutely can't stand this happening.

Solution method

Ask for help from the gods in the group.

Hang out in various forums and blogs looking for effective information

Finally, under the unremitting efforts of my husband, I found a library. It is go-linq, and using it can solve most of my needs for collections, make programming easier and work harder.

What is Linq?

LINQ (pronunciation: Link) is a language-level integrated query (Language INtegrated Query)

LINQ is a programming model for data access, which enables. Net language to directly support data query.

Linq is a cool syntax candy for C # programming, and everyone who has used it is impressed by its advanced features and partial natural semantics.

So here comes the question

So the question is, is there anything similar in go? the answer is yes, this time we are going to talk about library Go-linq. You will know what he does by this name. Say no more, just drive Lu.

Start using Go-LinqGo-Linq introduction

A powerful language that integrates Go for query (LINQ) libraries.

No dependence!

Use iterator mode to complete delay evaluation

It is safe for concurrent use.

Support for generic functions to make your code cleaner and free of type assertions

Supports arrays, slices, mappings, strings, channels, and custom collections

Use go get gopkg.in/ahmetb/go-linq.v3import. "gopkg.in/ahmetb/go-linq.v3"

Import. Means to use the library's methods directly instead of prefixes. Of course, you can also add, the official way of writing is like this.

Case

Define an employee class

Type Employee struct {Name string Age int Sex int / / 0 male and 1 female WorkYear int / / length of service}

Create different lists

Func initEmployeeData () [] Employee {list: = make ([] Employee, 0) for I: = 0; I < 10 List + {append (list, Employee {Name: "Zhang" + 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 List + {list = append (list, Employee {Name: "Zhang Yi", Age: 10, Sex: I% 2, WorkYear: 1 })} return list} pilot test knife-distinct removal starts func distinct () {var manEmpRows [] Employee rows: = initSameEployeeData () fmt.Println ("= = all employees whose gender is male list =") From (rows). Distinct (). ToSlice (& manEmpRows) fmt.Println (manEmpRows)}

The list of all employees whose gender is male is removed. [Zhang-10 01} {Zhang-10 1 1}]

The result is very nice, originally we need the tedious steps, a linq to solve, is not the Diao!

Search for thousands of degrees-where filter / / where filter condition var manEmpRows [] Employeefmt.Println ("= = filter employees whose gender is male = =") From (rows) .WhereT (func (e Employee) bool {return e.Sex = = 0}). ToSlice (& manEmpRows) fmt.Println (manEmpRows) won the Triple A-take+sort//Take selection of several elements from scratch fmt.Println ("filter gender is male") Select only the first two = = ") 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 filtering + sorting fmt.Println ("= = filtering employees whose gender is female and sorting by seniority = =") 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 filtering + double sorting fmt.Println ("= = filtering employees whose gender is female And sort by the descending order of seniority, and then sort by the ascending order of age = = ") From (rows) .WhereT (func (e Employee) bool {return e.Sex = = 1}) .OrderByDescing T (func (e Employee) int {return e.WorkYear}) .ThenByT (func (e Employee) int {return e.Age}). ToSlice (& manEmpRows) weak Water 3000 takes only one ladle-Select// only gets some fields in the element, and list outputs var outputRows [] stringfmt.Println (" = only get some fields in the element. List output = = ") From (rows) .SelectT (func (e Employee) string {return e.Name}). ToSlice (& outputRows) fmt.Println (outputRows) Colony-aggregation / / aggregation function 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 / / Get the first element or the last firstItem of the structure array: = From (rows). First () fmt.Println (firstItem) lastItem: = From (rows). Last () fmt.Println (lastItem) to this I believe you have a deeper understanding of "how to use Go-Linq". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report