Can't return *amqp.Channel from a function when using the Go RabbitMQ streadway/amqp driver -


i'm trying connect rabbitmq bus using streadway/amqp driver go. i'm working on reconnection routine and, it, have rabbitmqconsume function call rabbitmqconnect function.

func rabbitmqconnect(cfg objects.globalconfig) (*amqp.connection, *amqp.channel, error) {     rabbitconfig := amqp.config{         vhost:     cfg.rabbitvhost,         heartbeat: 5,     }      //open connection 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 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 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 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 value of ch , conn when they're returned rabbitmqconsume rabbitmqconnect, nil , program panics when run ch.consume line. i'm losely basing on this example ideas? thanks!

you have typo in error checking after amqp.dialconfig !

change err == nil err != nil

conn, err := amqp.dialconfig(url, rabbitconfig) if err != nil { // typed err == nil     return nil, nil, err } 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -