可以使用 Golang 的 encoding/json 库将结构体转换为 []byte,然后将其发送到 RabbitMQ。

例如,假设有一个结构体如下:

type Person struct {
    Name string 'json:"name"'
    Age int 'json:"age"'
}

可以使用以下代码将其转换为 []byte:

p := Person{'John', 30}
b, err := json.Marshal(p)
if err != nil {
    fmt.Println("error:", err)
}

然后将其发送到 RabbitMQ 即可。

注意:在发送消息时,需要指定消息的 Content-Type 为 application/json,以便接收方能够正确解析消息。

Golang RabbitMQ: 结构体转 []byte 发送消息

原文地址: https://www.cveoy.top/t/topic/lCDP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录