Go语言中文网 为您找到相关结果 34

golang将interface{}转换为struct

项目中需要用到golang的队列,container/list,需要放入的元素是struct,但是因为golang中list的设计,从list中取出时的类型为interface{},所以需要想办法把interface{}转换为struct。 这里需要用到interface assertion,具体操作见下面代码: 1 package main 2 3 import ( 4 "container/list" 5 "fmt" 6 "strconv" 7 ) 8 9 type People struct { 10 Name string 11 Age int 12 } 13 14 func main() { 15 // Create a new list and put some numbers ...阅读全文

博文 2017-12-28 19:00:07 lit10050528

golang消息队列实现用什么比较好?

项目开始需要与其他语言开发的项目通信,http请求比较损耗性能,但短时间内不太可能要求其他平台改成rpc通信。所以打算先用消息队列实现,网上搜了一下,好像golang用redis作消息队列比较少。 请问大家,golang 开发的项目还有什么其他的消息队列吗?如果是用redis做消息队列,会有什么问题吗...阅读全文

golang 用数组实现的循环队列

```go const maxSize = 100 //SqQueue 结构体定义 type SqQueue struct { data [maxSize]interface{} front int rear int } //New 新建空队列 func New() *SqQueue { return &SqQueue{ front: 0, rear: 0, } } // Length 队列长度 func (q *SqQueue) Length() interface{} { return (q.rear - q.front + maxSize) % maxSize } // Enqueue 入队 func (q *SqQueue) Enqueue(e interface{}) error ...阅读全文

博文 2019-07-18 15:26:57 daymenu

消息队列gmq

`gmq`是基于`redis`提供的特性,使用`go`语言开发的一个简单易用的队列;支持延迟任务,异步任务,超时任务,优先级任务 ### 应用场景 - 延迟任务 - 延迟任务,例如用户下订单一直处于未支付状态,半个小时候自动关闭订单 - 异步任务 - 异步任务,一般用于耗时操作,例如群发邮件等批量操作 - 超时任务 - 规定时间内`(TTR)`没有执行完毕或程序被意外中断,则消息重新回到队列再次被消费,一般用于数据比较敏感,不容丢失的 - 优先级任务 - 当多个任务同时产生时,按照任务设定等级优先被消费,例如a,b两种类型的job,优秀消费a,然后再消费b ### gmq流程 ![流程图](https://github.com/w...阅读全文

开源项目 2019-07-10 09:46:59 wuzhc

高性能、解耦、异步的微博的消息队列

离开微博已久,总想着弄点东西出来纪念一下当年的峥嵘岁月:)。在微博,你不能不知道鸟哥 Laruence ,也不能不知道 mq 。 mq 是一个基于 memcached 协议,用 c/c++编写的消息队列中间件,有着高性能、解耦、异步化等特点。现在用 Go 重新实现了一遍,将早前自己用 mq 的过程中觉得有些特性可以加上的,都一一加上了,由于其依赖的东西极少,只是简单用了一个轻巧型的嵌入式数据库 BerkeleyDB ,就可以实现一个 simple 的队列服务 tcp server ,见https://github.com/YoungPioneers/mgq, 特性如下,希望大家给些意见,么么哒。 1、一写多读:举个例子, set myqueue message ,只要 get 的时候...阅读全文

Go 异步任务队列 Go Machinery

Machinery 是一个 Go 语言的异步任务队列和作业队列,基于分布式消息传递。类似 Python 的 [Celery](http://www.oschina.net/p/celery) 框架。 Machinery 中的任务(或者作业)可通过多个 worker 在很多服务器上并发的执行,或者可在单个服务器上利用 Go 的协程运行多个 worker 进程。 ![Example worker receives tasks](http://static.oschina.net/uploads/img/201505/11070408_cuip.png) 任务的定义:

typ...阅读全文

开源项目 2015-05-10 16:00:00 RichardKnop

一款golang实现的高性能持久化消息队列

https://github.com/LevinLin/OPQOPQAn Open sourced Persistent message QueueCode is tested under go1.4.2 (CAUTION: OPQ hasn't been tested in production environment so far)Features1.persistent message storage2.push model - push message to target service and block when failure3.easy to use - simple API whith HTTP POST method, no addtional client integr...阅读全文

博文 2017-05-05 06:56:35 levin

Go 语言的异步任务队列和作业队列 AsynQ

## asynq 是一个 Go 语言的异步任务队列和作业队列,基于分布式消息传递。 ### asynq 中的任务(或者作业)可通过多个 worker 在很多服务器上并发的执行,或者可在单个服务器上利用 Go 的协程运行多个 worker 进程。来自谷歌员工2020年创建的标准库。 https://github.com/hibiken/asyn...阅读全文

博文 2020-01-18 00:48:00 paigel

【golang】队列

package datastructure import ( "errors" ) type Queue struct { list []int } func NewQueue() *Queue { list := make([]int, 0) return &Queue{list} } func (q *Queue) Enqueue(val int) { q.list = append(q.list, val) } func (q *Queue) Dequeue() int { if q.Len() == 0 { panic(errors.New("queue is empty")) } val := q.list[0] q.list = q.list[1:] return val } f...阅读全文

博文 2019-06-30 23:32:45 不想失忆的猫

Go 实现常见数据结构

1. [二叉搜索树](https://flaviocopes.com/golang-data-structure-binary-search-tree) 2. [字典](https://flaviocopes.com/golang-data-structure-dictionary) 3. [图](https://flaviocopes.com/golang-data-structure-graph) 4. [哈希表](https://flaviocopes.com/golang-data-structure-hashtable) 5. [链表](https://flaviocopes.com/golang-data-structure-linked-list) 6. [队列](h...阅读全文

go 无锁队列

无锁队列适用场景: 两个线程之间的交互数据, 一个线程生产数据, 另外一个线程消费数据,效率高缺点:需要使用固定分配的空间,不能动态增加/减少长度,存在空间浪费和无法扩展空间问题package main import ( "fmt" "reflect" "strings" "time" )type LoopQueue struct{ start int end int length int name string data []interface{} } func (this* LoopQueue)InitQueue(length int, name string)bool{ if nil == this || length <= 0{ return false } this.data = ...阅读全文

博文 2017-07-08 03:52:48 jinjiashan

数据结构-(队列顺序存储)循环队列的实现(golang)

import ( "errors" ) //循环队列结构体(顺序存储) type CirularQueue struct { elem []ElemType //储存的数据 front int //头指针 rear int //尾指针 } //初始化循环队列 func InitCQueue() *CirularQueue { cqueue := new(CirularQueue) cqueue.elem = make([]ElemType, MAXSIZE) cqueue.front = 0 cqueue.rear = 0 return cqueue } //清空循环队列 func (cqueue *CirularQueue) ClearCQueue() { cqueue.front = 0...阅读全文

博文 2017-02-09 16:59:44 卑微了承诺-life

Go 语言sync中waitgroup使用.小实例

package main import ( "fmt" "sync" "time" ) var waitgroup sync.WaitGroup func Test(x int) { fmt.Println(x) waitgroup.Done() //任务完成,将任务队列中的任务数量-1,其实.Done就是.Add(-1) } func main() { fmt.Println(time.Now()) for i := 0; i < 10; i++ { waitgroup.Add(1) go Test(i) } waitgroup.Wait() } golang中的同步是通过sync.WaitGroup来实现的.WaitGroup的功能:它实现了一个类似队列的结构,可以一直向队列中添加任务,...阅读全文

博文 2015-11-19 13:00:00 fyxichen

Golang 数据结构之【4.7环形队列】

package main import ( "errors" "fmt" ) //循环队列 const ( MaxSize int = 20 // 存储空间初始分配量 ) type QElemType int // QElemType类型根据实际情况而定,这里假设为int // 循环队列的顺序存储结构 type SqQueue struct { data [MaxSize]QElemType front int //头指针 rear int //尾指针 } //初始化队列 func (q *SqQueue) InitQueue() { q.front = 0 q.rear = 0 } //清空队列 func (q *SqQueue) ClearQueue() { q.front = 0 q....阅读全文

博文 2019-05-17 06:34:40 JiBadBoy

数据结构之静态队列go言语实现

package main import ( "fmt" ) type Queue struct { pBase *[6]int pFront,pRear int } func initQueue(q *Queue) { var arr=new([6]int) q.pBase=arr q.pFront=0 q.pRear=0 } func isEmpty(q *Queue) bool { if q.pFront==q.pRear { return true }else { return false } } func isFull(q *Queue) bool { if (q.pRear+1)%6==q.pFront { return true }else { return false } } ...阅读全文

博文 2016-11-18 02:00:01 tangguangqiang

golang heap container balance request

package main import ( "container/heap" "fmt" "log" "math/rand" "time" ) const ( MaxQueueLength = 10 MaxRequesters = 2 Seconds = 2e9 ) type Request func() func main() { requests := make(chan Request) for i := 0; i < MaxRequesters; i++ { go requester(requests) } NewBalancer(2).Balance(requests) } func requester(work chan Request) { for { time.Sleep(t...阅读全文

博文 2015-10-17 03:00:00 bnbqian

【栈与队列】由两个栈组成的队列

#### 【题目】 编写一个结构体,用两个栈实现队列,支持队列的基本操作(push、poll、peek) #### 【难度】 ★★☆☆ #### 【解答】 ```go package main import ( "container/list" "errors" "fmt" ) func main() { q := NewQueue() q.Push(1) fmt.Println(q.Peek()) q.Push(2) fmt.Println(q.Peek()) fmt.Println(q.Poll()) fmt.Println(q.Peek()) fmt.Println(q.Poll()) fmt.Println(q.Poll()) fmt.Println(q.Peek()) } type...阅读全文

归并排序的go语言与C++实现对比

最近对go语言发生了兴趣,发现go语言语法简洁,非常适合算法的描述和实现,于是对归并排序进行了实现。 例子中需要排序的队列是长度为100的从100到1的数列,排序算法是正序排序,排序正确的话,结果应当为1到100。 因为已设定数组最大值为100,因此“哨兵”简略设置为1000,因为不是算法核心部分,此处“哨兵”最大值处理省略。 1 /* 2 归并排序的go语言实现 3 */ 4 package main 5 6 import fmt "fmt" 7 8 func main () { 9 /* 10 生成需要排序的队列100~1 11 */ 12 length:=100 13 A:=make([]int,length) 14 j:=length 15 for i:=0;i阅读全文

博文 2016-09-11 09:00:02 bindong

数据结构-队列链式存储结构的实现(golang)

//队列的链式存储结构的实现 //队列链式存储结构体 type LSQueue struct { front *LSQNode //头指针 rear *LSQNode //尾指针 count int //队列的线性长度 } //队列链式存储结构节点 type LSQNode struct { elem ElemType //储存的数据 next *LSQNode //指向下一个节点的指针 lsQueue *LSQueue //所属队列 } //初始化队列 func InitLSQueue() *LSQueue { lsQueue := new(LSQueue) node := new(LSQNode) node.elem = nil node.next = nil node.lsQueue ...阅读全文

博文 2017-02-09 16:59:51 卑微了承诺-life

OPQ - 开源持久化消息队列

OPQ An Open sourced Persistent message Queue Code is tested under go1.4.2, higher version should be OK (CAUTION: OPQ hasn't been tested in production environment so far) Features persistent message storage push model - push message to target service and block when failure easy to use - simple API whith HTTP POST method, no addtional client integrat...阅读全文

博文 2017-04-19 06:00:51 levin

消息队列应用-使用异步队列就解耦了吗

消息队列作用一文介绍了为什么要使用消息队列。我们再来讨论下如何有效使用消息队列。 消息队列模式 目前主流消息队列主要由两种模式,1个是点对点模式,1个是发布订阅模式。简单做下介绍 点对点模式 1个消息只会被一个消费者消费。可以有一个或者多个消费者同时消费一个队列,但是被消费的消息不会重复。 发布订阅模式 消息可以被多个消费者消费。此类队列一般被称为一个topic,所有订阅该topic的consumer都可消费所有消息。 目前市面上有很多消息队列产品,ActiveMq,RabbitMQ,Kafka,ZeroMQ,golang实现的NSQ,支持的队列模式基本都可以以上面两个模式总结,只是实现方式不同,具体选型需要根据自己的使用场景选择。 比如kafka以其优异的性能应用在日志收集,hadoop技...阅读全文

博文 2019-08-25 23:32:52 刘凯_7013

golang 中操作nsq队列数据库

首先先在本地将服务跑起来,我用的是docker-compose ,一句话6666 先新建一个docker-compose.yml version: '2' services: nsqlookupd: image: nsqio/nsq command: /nsqlookupd ports: - "192.168.9.111:4160:4160" - "192.168.9.111:4161:4161" nsqd: image: nsqio/nsq command: /nsqd --lookupd-tcp-address=nsqlookupd:4160 depends_on: - nsqlookupd ports: - "192.168.9.111:4150:4150" - "192.168.9....阅读全文

博文 2017-09-02 07:30:03 jackluo

golang-nsq消息队列应用

消息队列由生产者和消费者共同协作,生产者产生消息放入队列中,消费者从队列中取出消息。 1.消费者 type Producer struct { id int64 addr string conn producerConn config Config logger logger logLvl LogLevel logGuard sync.RWMutex responseChan chan []byte errorChan chan []byte closeChan chan int transactionChan chan *ProducerTransaction transactions []*ProducerTransaction state int32 concurrentProduce...阅读全文

博文 2017-10-09 01:30:01 fwdqxl

Go hdu 4158 hoj 简单搜索

/*搜索中状态加入队列时就要立即更新标志, 不能出队的时候更新,否则中间有部分状态会重复搜索造成错误。*/ #include #include #include #include using namespace std; int map[21][21]; bool vis[21][21]; int dx[4]= {1,-1,0,0}; int dy[4]= {0,0,-1,1}; int cb,cw,n; struct point { int x,y; }p,t; void bfs(int x,int y) { p.x=x,p.y=y; queue q; q.push(p); int num=0; ...阅读全文

博文 2016-01-31 07:00:00 ehi11

golang使用数组模拟环形队列(demo)

// 定义环形队列结构体type LoopQueue struct { MaxSize int array [5]int front int rear int over bool // 标识队列是否溢出}// 定义环形队列的添加数据方法func (loop *LoopQueue) Add(val int) { // 环形队列如果队列满了会覆盖前面先进来的位置元素 if loop.rear == loop.MaxSize -1 { // 重置队尾的指针指向,从头重新开始队列的逻辑 // 这里如果用取模的话,还没实现 loop.rear = -1 // 这个是为了退出条件,当loop.rear == loop.front loop.front = loop.rear + 1 // 队列是否溢出覆...阅读全文

博文 2019-09-11 18:33:50 牛奶i豆浆

队列

如何理解队列 队列与栈做比较,就是队列是先进先出,队列本身就像一个管子一样。 队列 先进先出就是一个典型的队列。队列的应用十分广泛,特别是具有额外特性的队列,比如循环队列,阻塞队列,并发队列等,这些都是偏底层系统,框架,中间件的开发,都是有队列的身影,比如高性能的队列Disruptor、Linux环形缓存等。Java concurrent 并发包利用ArrayBlockingQueue 来实现公平锁等 如何实现一个队列 队列最基本的操作是 入队enqueue(),放一个数据到对尾;出队dequeue(),从队头取出一个元素。用数组实现的队列就是顺序队列,用链表实现的队列就是链式队列。 顺序队列 一个最基础的顺序队列实现(使用golang) type Queue struct { Items ...阅读全文

博文 2019-08-25 17:32:55 OOM_Killer

golang中goroutine的调度

基础的参考这篇文章 https://www.zhihu.com/question/20862617 线程 Machine (M) 处理器 Process (P) 协程 Goroutine (G) 要点 P为固定个数. 一般等于cpu核数 M > P. M为线程池中. G 有全局队列 和 P下的队列. 协作式 还是 抢占式 协程的意思大概就是协作式的线程的意思? 一开始的golang是协作式的. 就是说如果一个goroutine一直在执行.. 就让他执行下去吧 . 但是这会有一个问题. golang的gc是需要stop world的. 必须所有的goroutine都停下.. so... 没办法了. 而且也不能让一个goroutine一直占用P. 会把其他goroutine饿死... 所以 1....阅读全文

博文 2019-03-11 16:34:51 个00个

消息队列 NSQ 源码学习笔记 (三)

# NSQD 学习笔记 ## 特性总结 - 消息投放是不保序的 - 原因是内存队列、持久化队列、以及重新消费的数据混合在一起消费导致的 - 多个consumer 订阅同一个channel,消息将随机发送到不同的consumer 上 - 消息是可靠的 - 当消息发送出去之后,会进入`in_flight_queue` 队列 - 当恢复FIN 之后,才会从队列中将消费成功的消息清除 - 如果客户端发送REQ,消息将会重发 - 消息发送采用的是推模式,减少延迟 - 支持延迟消费的模式: **DPUB**, 或者 **RRQ** (消费未成功,延时消费) 命令 ## 代码学习 ### 程序入口 程序入口 `github.com/nsq/apps/nsqd/main.go` 1. 获取配置,并从meta...阅读全文

博文 2020-04-09 11:09:06 lpflpf

环形队列

PHP代码实现 { class Listo { public function getlist() { return $this->arr; } private $arr = array(); private $len = 0; private $head = 0; private $end = 0; public function init($len){ $this->len = $len; // 最后一个位置做区分用 for ($i=0; $i <= $len; $i++) { $this->arr[$i] = null; } } public function push($d){ $cur = $this->head; if (++$this->head > $this->len ) ...阅读全文

博文 2019-02-26 18:09:08 lobo