1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "github.com/streadway/amqp"
  6. )
  7. func failOnError(err error, msg string) {
  8. if err != nil {
  9. log.Fatalf("%s: %s", msg, err)
  10. panic(fmt.Sprintf("%s: %s", msg, err))
  11. }
  12. }
  13. func main() {
  14. conn, err := amqp.Dial("amqp://guest:guest@192.168.99.100:5672/")
  15. failOnError(err, "Failed to connect to RabbitMQ")
  16. defer conn.Close()
  17. ch, err := conn.Channel()
  18. failOnError(err, "Failed to open a channel")
  19. defer ch.Close()
  20. q, err := ch.QueueDeclare(
  21. "hello", // name
  22. false, // durable
  23. false, // delete when usused
  24. false, // exclusive
  25. false, // no-wait
  26. nil, // arguments
  27. )
  28. failOnError(err, "Failed to declare a queue")
  29. msgs, err := ch.Consume(
  30. q.Name, // queue
  31. "", // consumer
  32. true, // auto-ack
  33. false, // exclusive
  34. false, // no-local
  35. false, // no-wait
  36. nil, // args
  37. )
  38. failOnError(err, "Failed to register a consumer")
  39. forever := make(chan bool)
  40. go func() {
  41. for d := range msgs {
  42. log.Printf("Received a message: %s", d.Body)
  43. }
  44. }()
  45. log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
  46. <-forever
  47. }

请问《cloud.native.go》这段代码中goroutine为什么是run forever

https://github.com/cloudnativego/rabbit-hello/blob/master/receive.go