Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.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
Go Channel读写卡在无限循环中_Go_Channel_Beego - Fatal编程技术网

Go Channel读写卡在无限循环中

Go Channel读写卡在无限循环中,go,channel,beego,Go,Channel,Beego,首先,我想做一个长轮询通知系统。更具体地说,我将发出http请求,并且只有当map channel为true时才会返回响应 这是我使用的代码块: var MessageNotification = make(map[string]chan bool, 10) func GetNotification(id int, timestamp int) notification { <-MessageNotification["1"] var chat_services []*

首先,我想做一个长轮询通知系统。更具体地说,我将发出http请求,并且只有当map channel为
true
时才会返回响应

这是我使用的代码块:

var MessageNotification = make(map[string]chan bool, 10)

func GetNotification(id int, timestamp int) notification {
    <-MessageNotification["1"]

    var chat_services []*models.Chat_service
    o := orm.NewOrm()

    _, err := o.QueryTable("chat_service").Filter("Sender__id", id).RelatedSel().All(&chat_services)

    if err != nil {
        return notification{Status: false}
    }
    return notification{Status: true, MessageList: chat_services}
}

func SetNotification(id int) {
    MessageNotification[strconv.Itoa(id)] <- true
}
为测试创建的函数名和变量


没有发生错误。感谢您的帮助。

您没有创建频道

var MessageNotification = make(map[string]chan bool, 10)
这条线制作了一张容量为10的地图,但您并没有在地图中创建实际的频道。因此,`SetNotification[“1”]是一个nil通道,在nil通道上发送和接收数据是无限期的

你需要投入

MessageNotification["1"] = make(chan bool)
如果需要,您可以添加一个大小(我有一个预感,您在地图制作中的“10”应该是该频道的缓冲)。这甚至可以有条件地做到:

func GetNotification(id int, timestamp int) notification {
    if _, ok := MessageNotification["1"]; !ok { // if map does not contain that key
        MessageNotification["1"] = make(chan bool, 10)
    }

    <-MessageNotification["1"]
    // ...
}

func SetNotification(id int) {
    if _, ok := MessageNotification[strconv.Itoa(id)]; !ok { // if map does not contain that key
        MessageNotification[strconv.Itoa(id)] = make(chan bool, 10)
    }

    MessageNotification[strconv.Itoa(id)] <- true
}
func GetNotification(id int,timestamp int)通知{
如果{,确定:=MessageNotification[“1”];!确定{//如果映射不包含该键
MessageNotification[“1”]=make(chan bool,10)
}

你没有创建你的频道

var MessageNotification = make(map[string]chan bool, 10)
此行生成了一个容量为10的映射,但您没有在映射中创建实际的通道。因此,`SetNotification[“1”]是一个nil通道,并在nil通道块上无限期地发送和接收

你需要投入

MessageNotification["1"] = make(chan bool)
如果需要,您可以包括一个大小(我有一个预感,您在地图制作中的“10”应该是该通道的缓冲)。这甚至可以有条件地完成:

func GetNotification(id int, timestamp int) notification {
    if _, ok := MessageNotification["1"]; !ok { // if map does not contain that key
        MessageNotification["1"] = make(chan bool, 10)
    }

    <-MessageNotification["1"]
    // ...
}

func SetNotification(id int) {
    if _, ok := MessageNotification[strconv.Itoa(id)]; !ok { // if map does not contain that key
        MessageNotification[strconv.Itoa(id)] = make(chan bool, 10)
    }

    MessageNotification[strconv.Itoa(id)] <- true
}
func GetNotification(id int,timestamp int)通知{
如果{,确定:=MessageNotification[“1”];!确定{//如果映射不包含该键
MessageNotification[“1”]=make(chan bool,10)
}

你如何调用
SetNotification
?你的代码中没有循环,所以它可能是一个阻塞通道。你能分享更多的代码吗?我添加了代码。你认为我需要循环吗?你如何调用
Notification
Websocket
函数?这些是控制器。我使用beego框架和beego回调控件根据url的oller函数(例如localhost:8080/chatinfo)你应该在你的问题中添加beego标签。知道它的人可能会提供帮助。你如何调用
SetNotification
?你的代码中没有循环,因此可能是一个阻塞通道。你能分享更多的代码吗?我添加了代码。你认为我需要循环吗?你如何调用
Notification
网站ocket
函数?这些是控制器。我根据url使用beego框架和beego回调控制器函数。(例如localhost:8080/chatinfo)您应该在问题中添加beego标记。知道的人可能会提供帮助。