当前位置:网站首页>go 多线程数据搜索
go 多线程数据搜索
2022-07-02 22:07:00 【给我一瓶冰阔洛】
go 多线程数据搜索
package main
import (
"fmt"
"strings"
"time"
)
type User struct {
Email string
Name string
}
var DataBase = []User{
{"[email protected]","张三"},
{"[email protected]","张李四"},
{"[email protected]","王五"},
{"[email protected]","赵六"},
{"[email protected]","李七"},
{"[email protected]","武松"},
{"[email protected]","林冲"},
{"[email protected]","晁盖"},
{"[email protected]","李逵"},
{"[email protected]","宋江"},
{"[email protected]","鲁智深"},
{"[email protected]","吴用"},
}
type Worker struct {
users []User
ch chan *User
name string
}
func NewWorker(users []User, ch chan *User, name string)*Worker{
return &Worker{
users: users,
ch: ch,
name: name,
}
}
func (w*Worker)Find(email string){
for i:=range w.users{
user := &w.users[i]
if strings.Contains(user.Email, email){
w.ch <- user
}
}
}
func main() {
email := "sdfg5sfdg5"
ch := make(chan *User)
go NewWorker(DataBase[:3], ch,"吴用").Find(email)
go NewWorker(DataBase[3:8], ch,"吴用").Find(email)
go NewWorker(DataBase[8:], ch,"吴用").Find(email)
for {
select {
case user := <-ch:
fmt.Println(user.Email,user.Name)
return
case <-time.After(time.Second):
fmt.Println("find-ing")
}
}
}
边栏推荐
- Market Research - current market situation and future development trend of intravenous injection (IV) bottles
- Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
- What is the'function'keyword used in some bash scripts- What is the 'function' keyword used in some bash scripts?
- Simpleitk use - 3 Common operations
- U++ 学习笔记 堆
- [autosar-dcm] - 4.3-how UDS $22 and $2e services read and write NVM data
- Notes on key vocabulary of the original English book biography of jobs (IX) [chapter seven]
- [error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')
- U++ learning notes - relaxation
- 影视随摘
猜你喜欢
Task and privilege level protection
Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
Perceptron model and Application
Dynamic memory allocation (malloc calloc realloc free)
Graphic view frame
NC50965 Largest Rectangle in a Histogram
Struct, bit segment, enumeration, union
[foreign journal] sleep and weight loss
建立自己的网站(22)
wait解决僵尸进程
随机推荐
[QT] QT multithreading development - four methods to realize multithreading design
UE4 UI adaptive screen
基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习
[001] [arm-cortex-m3/4] internal register
Oracle-PL/SQL编程
Film and television excerpts
全面解析分享购商业模式逻辑?分享购是如何赋能企业
How can I use knockout's $parent/$root pseudovariables from inside a . computed() observable?
Socket套接字C/S端流程
php实现根据输入的年龄查询出生日期符合的数据
Methods of adding styles to native JS
[QT] Q multithreaded development - Analysis of multithreaded application examples (Mandelbrot)
Radis:Linux上安装Redis(步骤)
Pointer array parameter passing, pointer parameter passing
用sentinel熔断比例阈值改不了,设置慢调用比例没效果
#include errors detected. Please update your includePath.
Market Research - current situation and future development trend of herringbone gear Market
U++ learning notes - relaxation
Build your own website (22)
Mathematical modeling -- graph and network models and methods (I)