dprq18175 2016-11-10 18:44
浏览 172
已采纳

使用Go RabbitMQ streadway / amqp驱动程序时无法从函数返回* amqp.Channel

I'm trying to connect to a RabbitMQ bus using the streadway/amqp driver for Go. I'm working on a reconnection routine and, for it, I have a rabbitMQConsume function call a rabbitMQConnect function.

func rabbitMQConnect(cfg objects.GlobalConfig) (*amqp.Connection, *amqp.Channel, error) {
    rabbitConfig := amqp.Config{
        Vhost:     cfg.RabbitVHost,
        Heartbeat: 5,
    }

    //Open connection to Rabbit
    url := fmt.Sprintf("amqp://" + cfg.RabbitUser + ":" + cfg.RabbitPassword + "@" + cfg.RabbitHost + ":" + cfg.RabbitPort + cfg.RabbitVHost)

    conn, err := amqp.DialConfig(url, rabbitConfig)
    if err == nil {
        return nil, nil, err
    }

    ch, err := conn.Channel()
    if err != nil {
        return nil, nil, err
    }
    // Create Exchange if it doesn't exist
    err = ch.ExchangeDeclare(
        "ali",    // name
        "direct", // type
        true,     // durable
        false,    // auto-deleted
        false,    // internal
        false,    // no-wait
        nil,      // arguments
    )
    if err != nil {
        return nil, nil, err
    }

    //Declare queue
    _, err = ch.QueueDeclare(
        cfg.RabbitQueue, // name
        true,            // durable
        false,           // delete when usused
        false,           // exclusive
        false,           // no-wait
        nil,             // arguments
    )
    if err != nil {
        return nil, nil, err
    }

    //Bind queue
    err = ch.QueueBind(
        cfg.RabbitQueue,    // queue name
        cfg.RabbitKey,      // routing key
        cfg.RabbitExchange, // exchange
        false,
        nil,
    )
    if err != nil {
        return nil, nil, err
    }
    return conn, ch, nil
}

//RabbitMQConsume setup the channel/exchange data
func rabbitMQConsume(cfg objects.GlobalConfig) (*amqp.Connection, <-chan amqp.Delivery, error) {
    conn, ch, err := rabbitMQConnect(cfg)
    if err != nil {
        return nil, nil, err
    }

    consumerID, err := helper.GetConsumerID()
    if err != nil {
        return nil, nil, err
    }

    //Start receiving data in the msgs channel
    msgs, err := ch.Consume(
        cfg.RabbitQueue, // queue
        consumerID,      // consumer
        false,           // auto-ack
        false,           // exclusive
        false,           // no-local
        false,           // no-wait
        nil,             // args
    )
    if err != nil {
        return nil, nil, err
    }

    return conn, msgs, nil
}

The problem I'm having is that the value of ch and conn when they're returned to rabbitMQConsume from rabbitMQconnect, are nil and the program panics when I run the ch.Consume line. I'm losely basing this on this example Any ideas? Thanks!

  • 写回答

1条回答 默认 最新

  • duanchuo7741 2016-11-11 09:29
    关注

    You have a typo in your error checking after amqp.DialConfig !

    Change the err == nil to err != nil

    conn, err := amqp.DialConfig(url, rabbitConfig)
    if err != nil { // you typed it as err == nil
        return nil, nil, err
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题