title: Golang - RabbitMq : channel/connection is not open
body: <p>I'm new to golang, and I would like to refactorate my code so that the rabbitmq initialization is in another function that main. So I use a struct pointer (containing all the rabbitmq infos initilized) and pass it to the send function, but it tells me : Failed to publish a message: Exception (504) Reason: "channel/connection is not open"</p>
  
  <p>struct :</p>
  
  <pre><code>type RbmqConfig struct {
      q amqp.Queue
      ch *amqp.Channel
      conn *amqp.Connection
      rbmqErr error
  }
  </code></pre>
  
  <p>the init function :</p>
  
  <pre><code>func initRabbitMq() *RbmqConfig {
  
      config := &amp;RbmqConfig{}
  
      config.conn, config.rbmqErr = amqp.Dial("amqp://guest:guest@localhost:5672/")
      failOnError(config.rbmqErr, "Failed to connect to RabbitMQ")
      defer config.conn.Close()
  
      config.ch, config.rbmqErr = config.conn.Channel()
      failOnError(config.rbmqErr, "Failed to open a channel")
      defer config.ch.Close()
  
      config.q, config.rbmqErr = config.ch.QueueDeclare(
          "&lt;my_queue_name&gt;",
          true,   // durable
          false,   // delete when unused
          false,   // exclusive
          false,   // no-wait
          nil,     // arguments
      )
      failOnError(config.rbmqErr, "Failed to declare a queue")
  
      return config
  }
  </code></pre>
  
  <p>main :</p>
  
  <pre><code>config := initRabbitMq()
  
  fmt.Println("queue name : ", config.q.Name)
  
  sendMessage(config, &lt;message_to_send&gt;)
  </code></pre>
  
  <p>in send message :</p>
  
  <pre><code>func sendMessage(config *RbmqConfig, &lt;message_to_send&gt;) {
  
      config.rbmqErr = config.ch.Publish(
          "",           // exchange
          config.q.Name,       // routing key
          false,        // mandatory
          false,
          amqp.Publishing{
              DeliveryMode: amqp.Persistent,
              ContentType:  "text/plain",
              Body:         []byte(&lt;message_to_send&gt;),
          })
      failOnError(config.rbmqErr, "Failed to publish a message")
  </code></pre>
  
  <p>If someone has any idea, that would be very helpful. Thank you in advance</p>