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 :
- Créez un nouveau dossier et initialisez votre module Go :
go mod init rabbitmq-example
- Ajoutez ensuite la bibliothèque RabbitMQ à votre projet :
go get github.com/rabbitmq/amqp091-go
-
Créez maintenant un fichier nommé
main.goet 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") } -
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.