title: Could one connection support multiple channels in go api for rabbitmq?
body: <pre><code>package main
  
  import (
  "fmt"
  "github.com/streadway/amqp"
  "time"
  )
  
  // Every connection should declare the topology they expect
  func setup(url, queue string) (*amqp.Connection, *amqp.Channel, error) {
      //setup connection
      conn, err := amqp.Dial(url)
      if err != nil {
          return nil, nil, err
      }
      //build channel in the connection
      ch, err := conn.Channel()
          if err != nil {
          return nil, nil, err
      }
      //queue declare
      if _, err := ch.QueueDeclare(queue, false, true, false, false, nil); err != nil {
          return nil, nil, err
      }
  
      return conn, ch, nil
  }
  
  func main() {
      //amqp url
      url := "amqp://guest:guest@127.0.0.1:5672";
      for i := 1; i &lt;= 2; i++ {
          fmt.Println("connect ", i)
          //two goroutine   
          go func() {
              //queue name
              queue := fmt.Sprintf("example.reconnect.%d", i)
              //setup channel in the tcp connection
              _, pub, err := setup(url, queue)
              if err != nil {
                  fmt.Println("err publisher setup:", err)
                  return
              }
              // Purge the queue from the publisher side to establish initial state
              if _, err := pub.QueuePurge(queue, false); err != nil {
                  fmt.Println("err purge:", err)
                  return
              }
              //publish msg
              if err := pub.Publish("", queue, false, false, amqp.Publishing{
                  Body: []byte(fmt.Sprintf("%d", i)),
              }); err != nil {
                  fmt.Println("err publish:", err)
                  return
              }
              //keep running
              for{
                  time.Sleep(time.Second * 20)
              }
          }()
      }
      //keep running
      for {
          time.Sleep(time.Second * 20)
      }
  }
  </code></pre>
  
  <p>I thought there is only one connection between the program and mq-server,</p>
  
  <p>but there are two connection,one connection can only support one channel,why?</p>
  
  <p>can't the two goroutine share the same tcp connection?</p>
  
  <p>Socket descriptor can share in all threads of a process in the theory.</p>
  
  <p>Why the two goroutine don't share one socket but have their own channel?</p>
  
  <p>The model by hand:</p>
  
  <p><img src="https://i.stack.imgur.com/YD21Y.png" alt="model1"></p>
  
  <p>The real model in rabbitmq:
  <img src="https://i.stack.imgur.com/s91gE.png" alt="model2"></p>