Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript golang的pubsub备选方案_Javascript_Go_Publish Subscribe_Channel - Fatal编程技术网

Javascript golang的pubsub备选方案

Javascript golang的pubsub备选方案,javascript,go,publish-subscribe,channel,Javascript,Go,Publish Subscribe,Channel,我已经使用pubsub在javascript中完成了一项简单的任务,任务如下: 我有两个对象,一个是A,另一个是对某个元素感兴趣的对象(本例中为字符串),一个是Foo对元素m,n感兴趣的对象,另一个是Bar对元素n,o,p感兴趣的对象。利益可以交叉 A对象具有添加/删除元素的方法,当该对象包含m,n元素且Foo感兴趣时,则该对象存储在Foo中,这是使用pubsub的javascript中的伪代码 var A = {}; var Foo = { interests: ['m', 'n']

我已经使用pubsub在javascript中完成了一项简单的任务,任务如下:

我有两个对象,一个是
A
,另一个是对某个元素感兴趣的对象(本例中为字符串),一个是
Foo
对元素
m,n
感兴趣的对象,另一个是
Bar
对元素
n,o,p
感兴趣的对象。利益可以交叉

A对象具有添加/删除元素的方法,当该对象包含
m,n
元素且
Foo
感兴趣时,则该对象存储在
Foo
中,这是使用pubsub的javascript中的伪代码

var A = {};

var Foo = {
    interests: ['m', 'n'],
    storedObj: {},
    tempObj: {}
};

// Bar same as Foo with different interest ['n', 'o', 'p']

// somewhere in Foo and Bar constructor
// Foo and Bar subscribe too each interests element
// for each interests when add
subscribe('add'+interest, function(obj) {
    // store this obj in tempObj and increment until satisfy all 
    // interest
    tempObj[obj]++;

    // if this obj satisfy all interest then store it in array of obj
    if(tempObj[obj] === len(interests)) {
        storedObj[obj] = true;
    }
});

// for each interests when remove
subscribe('remove'+interest, function(obj) {
    // remove from storedObj
    delete storedObj[obj];

    // decrement tempObj so it can be used for later if the interest 
    // is adding again
    tempObj[obj]--;
});

// inside A prototype
prototype.add = function(interest) {
    publish('add'+interest, this);
    return this;
}
prototype.remove = function(interest) {
    publish('remove'+interest, this);
    return this;
}

// implementation
A.add('m')
 .add('n')
 .add('o')

// then A is stored inside Foo but not in Bar because A doesn't have 
// `p`, but it still stored Bar.tempObj and have value 2 and waiting 
// for `p` to be add

A.remove('m')
 .add('p')

// then A is removed from Foo and stored in Bar
我想把这个任务移植到golang中,但我不想使用pubsub,我希望golang的方式更习惯。注意:我已经在golang使用了pubsub


你能告诉我在戈朗怎么做吗?我正在使用channel,但找不到解决方案。

只是给你一个想法,不一定是你真正的用例

package main

import (
    "fmt"
    "time"
)

type Publisher struct {
    subscription map[string]chan string
}

func (p *Publisher)Subscribe(interest string) chan string{
    if p.subscription == nil {
        p.subscription = make(map[string]chan string)
    }
    p.subscription[interest] = make(chan string)
    return p.subscription[interest]
}

func (p *Publisher) Add(val string) {
    if p.subscription[val] != nil {
        fmt.Println("Adding " + val)
        p.subscription[val] <- "added " + val
    }
}
func (p *Publisher) Remove(val string) {
    if p.subscription[val] != nil {
        p.subscription[val] <- "removed " + val
    }
}

type Subscriber struct {
    subscriptions [] chan string
    publisher *Publisher
}

func (s *Subscriber) RegisterInterest(interest string){
    s.subscriptions = append(s.subscriptions, s.publisher.Subscribe(interest))
}
func (s *Subscriber) run(channel chan string) {
    for  {
        fmt.Println("Waiting for message")
        m := <- channel
        fmt.Println("Got message : " + m)
    }
}
func (s *Subscriber) Listen()  {
    for _, elem := range s.subscriptions {
        go s.run(elem)
    }

}
func main() {
    pub := Publisher{}
    sub := &Subscriber{publisher: &pub}
    sub.RegisterInterest("m")
    sub.RegisterInterest("n")
    sub.Listen()
    pub.Add("m")
    pub.Add("n")
    pub.Remove("m")
    pub.Remove("n")
    time.Sleep(time.Second * 10)
}
主程序包
进口(
“fmt”
“时间”
)
类型发布器结构{
订阅地图[字符串]更改字符串
}
func(p*Publisher)订阅(兴趣字符串)chan字符串{
如果p.subscription==nil{
p、 订阅=制作(映射[字符串]更改字符串)
}
p、 认购[利息]=make(成串)
返回p.认购[利息]
}
func(p*Publisher)添加(val字符串){
如果p.subscription[val]!=nil{
格式打印项次(“添加”+val)
p、 订阅[val]您可以使用工作队列。