如何从Golang中的MongoDB bson.m中制作多行字符串值?

发布于 2025-02-06 14:24:42 字数 740 浏览 1 评论 0原文

我正在使用Golang创建电报机器人,我需要一些有关如何从Golang中检索多行字符串值的建议,该函数具有与此Python String相同的逻辑,

answer = """1 John 95
2 Sam 89
3 Mike 72"""

我具有一个函数,该函数创建了一个mongoDB请求,该请求使我获取数据在bson.m数据类型中。之后,我需要使用此表格将此查询数据作为单个字符串值发送给用户:

msg := tgbotapi.NewMessage(
    update.Message.Chat_ID,
    answer,
)
bot.Send(msg)

我真的不知道如何将此bson.m数据转换为单个多行字符串。

我从功能中获得的bson.m响应:

[map[_id:ObjectID("62a4acf2a494a2814238c6e1") bandMember:John name:School12 points:95] 
map[_id:ObjectID("62a4acf2a494a2814238c6e2") bandMember:Sam name:School15 points:89] 
map[_id:ObjectID("62a4acf2a494a2814238c6e3") bandMember:Mike name:School7 points:72]]

我必须将其插入“答案”的字符串变量(请参见上面的示例),

谢谢!

I'm creating a telegram bot using Golang, and I need some advice on how to retrieve a multiline string value from function in Golang, that has same logic like this Python string

answer = """1 John 95
2 Sam 89
3 Mike 72"""

I have a function that creates a MongoDB request that gets me a data in bson.M datatype. And after that I need to send this queried data to a user as a single string value using this form:

msg := tgbotapi.NewMessage(
    update.Message.Chat_ID,
    answer,
)
bot.Send(msg)

I don't really know how to transform this bson.M data into a single multiline string.

bson.M response that I get from the function:

[map[_id:ObjectID("62a4acf2a494a2814238c6e1") bandMember:John name:School12 points:95] 
map[_id:ObjectID("62a4acf2a494a2814238c6e2") bandMember:Sam name:School15 points:89] 
map[_id:ObjectID("62a4acf2a494a2814238c6e3") bandMember:Mike name:School7 points:72]]

And I have to insert it in a string variable of "answer" (see above example)

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿看小鸭子会跑 2025-02-13 14:24:42

循环浏览文档,并为每个文档提供一条线。加入Newline以获得最终结果。

var lines []string
for i, m := range data {
    lines = append(lines, fmt.Sprintf("%d %s %d", i+1, m["bandMember"], m["points"]))
}
result := strings.Join(lines, "\n")

Loop through the documents and sprintf a line for each document. Join the lines with newline to get the final result.

var lines []string
for i, m := range data {
    lines = append(lines, fmt.Sprintf("%d %s %d", i+1, m["bandMember"], m["points"]))
}
result := strings.Join(lines, "\n")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文