```go
func failOnError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
}
}
func main() {
// 创建连接
conn, err := amqp.Dial("amqp://admin:admin@localhost:5672/")
failOnError(err, "Failed to connect to RabbitMQ")
defer conn.Close()
// 创建连接通道
ch, err := conn.Channel()
failOnError(err, "Failed to open a channel")
defer ch.Close()
// 如果没有消息队列,创建消息队列
_, err = ch.QueueDeclare(
"AUDITLOG_TOPIC_client-40009",
true,
false,
false,
false,
nil,
)
failOnError(err, "Failed to declare a queue")
// 从消息队列中获取消息
msgs, err := ch.Consume(
"AUDITLOG_TOPIC_client-40009",
"",
true,
false,
false,
false,
nil,
)
failOnError(err, "Failed to register a consumer")
forever := make(chan bool)
// 创建协程获取消息队列
go func() {
for d := range msgs {
// json数据转换为logDto
var logDto dto.LogDto
err = json.Unmarshal(d.Body, &logDto)
failOnError(err, "Failed to unmarshal json")
// logDto转换为logModel
logModel := convertor.LogDtoTogModel(logDto)
service.InsertLogService(logModel)
}
}()
// 为什么不会deadlock???
<-forever
}
```
<a href="/user/__Golang__" title="@__Golang__">@__Golang__</a> 确实是这样,感谢解惑。请问是否知道这样的原理吗
#8
更多评论