RabbitMQ: Utiliser GoLang pour se connecter à RabbitMQ

Cette documentation fait partie du guide Premiers pas. Consultez le guide complet ici : Comment utiliser Stackhero pour RabbitMQ.

Pour connecter une application Go à RabbitMQ, la Go RabbitMQ Client Library officielle simplifie la démarche. Voici comment commencer :

  1. Créez un nouveau dossier et initialisez votre module Go :
go mod init rabbitmq-example
  1. Ajoutez ensuite la bibliothèque RabbitMQ à votre projet :
go get github.com/rabbitmq/amqp091-go
  1. Créez maintenant un fichier nommé main.go et ajoutez ce code :

    package main
    
    import (
      "fmt"
      amqp "github.com/rabbitmq/amqp091-go"
    )
    
    func main() {
      connection, err := amqp.Dial("amqps://<PASSWORD>@<XXXXXX>.stackhero-network.com:<AMQP_PORT_TLS>")
      if err != nil {
        panic(err)
      }
      defer connection.Close()
    
      fmt.Println("Successfully connected to RabbitMQ instance")
    }
    
  2. Exécutez votre code avec :

go run main.go

Si la connexion fonctionne, vous verrez le message "Successfully connected to RabbitMQ instance". Cela signifie que vous êtes connecté de façon sécurisée avec authentification et chiffrement TLS.

Pour des exemples plus avancés, consultez les exemples Go du dépôt officiel RabbitMQ : https://github.com/rabbitmq/rabbitmq-tutorials/tree/main/go.