英文:
pubsub alternative in golang
问题
我有一个已经用JavaScript和pubsub完成的简单任务,任务如下:
我有一个对象,假设为A
,还有另外两个对象对某些元素(在这种情况下是字符串)感兴趣,假设Foo
对元素m
和n
感兴趣,Bar
对元素n
、o
和p
感兴趣。这些兴趣可以相互交叉。
对象A
有添加/删除元素的方法,当该对象包含Foo
感兴趣的元素m
和n
时,该对象存储在Foo
中。以下是使用pubsub的JavaScript伪代码:
var A = {};
var Foo = {
interests: ['m', 'n'],
storedObj: {},
tempObj: {}
};
// Bar与Foo类似,但兴趣不同,为['n', 'o', 'p']
// 在Foo和Bar的构造函数中的某个地方
// Foo和Bar订阅每个兴趣元素
// 对于每个兴趣,在添加时
subscribe('add'+interest, function(obj) {
// 在tempObj中存储此对象并递增,直到满足所有兴趣
tempObj[obj]++;
// 如果此对象满足所有兴趣,则将其存储在对象数组中
if(tempObj[obj] === len(interests)) {
storedObj[obj] = true;
}
});
// 对于每个兴趣,在删除时
subscribe('remove'+interest, function(obj) {
// 从storedObj中删除
delete storedObj[obj];
// 将tempObj递减,以便在兴趣再次添加时可以使用
tempObj[obj]--;
});
// 在A的原型中
prototype.add = function(interest) {
publish('add'+interest, this);
return this;
}
prototype.remove = function(interest) {
publish('remove'+interest, this);
return this;
}
// 实现
A.add('m')
.add('n')
.add('o')
// 然后A被存储在Foo中,但不在Bar中,因为A没有`p`,但它仍然存储在Bar.tempObj中,并且具有值2,等待添加`p`
A.remove('m')
.add('p')
// 然后A从Foo中移除并存储在Bar中
我想将这个任务移植到Go语言中,但我不想使用pubsub,我想使用更符合Go语言习惯的方式。注意:我已经在Go语言中使用pubsub完成了这个任务。
你能告诉我如何在Go语言中实现吗?我考虑使用通道,但是找不到解决方案。
英文:
I have simple task that already done in javascript using pubsub, here is the task:
I have object let say A
and another 2 object that have interest in some element(string in this case), let say Foo
interest in element m, n
and Bar
interest in element n, o, p
. The interest can intersect.
The A object have method adding/remove element and when that object contain m, n
element which Foo
interest in, then that object stored in Foo
here's the pseudo code in javascript using pubsub
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
I want to porting this task into golang but i don't want using pubsub, i want more idiomatic to golang way. NOTE: i already done using pubsub in golang as well.
Can you show me how to do it in golang? i'm thingking using channel, but and can't find the solution.
答案1
得分: 1
只是给你一个想法,不一定是你真正的用例。
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)
}
希望对你有所帮助!
英文:
Just to give you an idea, not necessarily your real use case.
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论