当前位置:网站首页>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")
}
}
}
边栏推荐
- 【微服务|Sentinel】重写sentinel的接口BlockExceptionHandler
- 'when to use const char * and when to use const char []' - when to use const char * and when to use const char []
- Notes on key vocabulary in the English original of the biography of jobs (10) [chapter eight]
- Notes on key vocabulary of the original English book biography of jobs (IX) [chapter seven]
- Market Research - current situation and future development trend of preclinical medical device testing service market
- Methods of adding styles to native JS
- 送给即将工作的自己
- Solve the error of changing the selected file when uploading excel file. Net:: err_ UPLOAD_ FILE_ CHANGED
- 杰理之修改不需要长按开机功能【篇】
- SimpleITK使用——3. 常见操作
猜你喜欢

Oracle cursor

悬镜安全在RSAC2022上斩获Global InfoSec Awards四项大奖

大话云原生之负载均衡篇-小饭馆客流量变大了

任务和特权级保护

SimpleITK使用——4. 奇怪的問題

Simpleitk use - 4 Strange question

Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts

Niuke: Dragon and dungeon games

手写ORM(对象关系映射)增删改查

Leetcode circular linked list (fast and slow pointer) code line by line interpretation
随机推荐
Server response status code
U++ 原始内存 学习笔记
Market Research - current market situation and future development trend of total nutrition products
wait解决僵尸进程
PHP微信抢红包的算法
傑理之修改不需要長按開機功能【篇】
【外刊】睡眠与减肥
杰理之如何测试按键的误触率【篇】
建立自己的网站(22)
Notes on key vocabulary in the English original of the biography of jobs (11) [chapter nine]
杰理之、产线装配环节【篇】
[foreign journal] sleep and weight loss
Market Research - current market situation and future development trend of aircraft wireless intercom system
Regular expression (2)
影视随摘
How can I use knockout's $parent/$root pseudovariables from inside a . computed() observable?
JS solution for obtaining the width and height of hidden elements whose display is none
Market Research - current situation and future development trend of carob chocolate market
Market Research - current situation and future development trend of cell-based seafood market
Oracle-游标