正确的结构切片保存方法到 Cloud Datastore(Firestores数据存储模式下)是什么?
来源:stackoverflow
时间:2024-03-09 15:18:26 372浏览 收藏
Golang不知道大家是否熟悉?今天我将给大家介绍《正确的结构切片保存方法到 Cloud Datastore(Firestores数据存储模式下)是什么?》,这篇文章主要会讲到等等知识点,如果你在看完本篇文章后,有更好的建议或者发现哪里有问题,希望大家都能积极评论指出,谢谢!希望我们能一起加油进步!
我想在 google cloud datastore(数据存储模式下的 firestore)中保存一部分结构。
以此电话簿和联系人为例。
type contact struct {
key *datastore.key `json:"id" datastore:"__key__"`
email string `json:"email" datastore:",noindex"`
name string `json:"name" datastore:",noindex"`
}
type phonebook struct {
contacts []contact
title string
}
保存和加载此结构没有问题,因为数据存储库会处理它。
由于我的实际代码中存在一些复杂的属性,因此我需要实现 propertyloadsaver 方法。
保存 title 属性非常简单。但我在存储 contact 结构切片时遇到问题。
我尝试使用 savestruct 方法:
func (pb *phonebook) save() ([]datastore.property, error) {
ps := []datastore.property{
{
name: "title",
value: pb.title,
noindex: true,
},
}
ctt, err := datastore.savestruct(pb.contacts)
if err != nil {
return nil, err
}
ps = append(ps, datastore.property{
name: "contacts",
value: ctt,
noindex: true,
})
return ps, nil
}
此代码可以编译,但不起作用。
错误消息为datastore:无效实体类型
显式制作 property 切片也不起作用:
func (pb *phonebook) save() ([]datastore.property, error) {
ps := []datastore.property{
{
name: "title",
value: pb.title,
noindex: true,
},
}
cttprops := datastore.property{
name: "contacts",
noindex: true,
}
if len(pb.contacts) > 0 {
props := make([]interface{}, 0, len(pb.contacts))
for _, contact := range pb.contacts {
ctt, err := datastore.savestruct(contact)
if err != nil {
return nil, err
}
props = append(props, ctt)
}
cttprops.value = props
}
ps = append(ps, cttprops)
return ps, nil
}
制作实体切片也不起作用:
func (pb *phonebook) save() ([]datastore.property, error) {
ps := []datastore.property{
{
name: "title",
value: pb.title,
noindex: true,
},
}
cttprops := datastore.property{
name: "contacts",
noindex: true,
}
if len(pb.contacts) > 0 {
values := make([]datastore.entity, len(pb.contacts))
props := make([]interface{}, 0, len(pb.contacts))
for _, contact := range pb.contacts {
ctt, err := datastore.savestruct(contact)
if err != nil {
return nil, err
}
values = append(values, datastore.entity{
properties: ctt,
})
}
for _, v := range values {
props = append(props, v)
}
cttprops.value = props
}
ps = append(ps, cttprops)
return ps, nil
}
两者都产生了相同的错误 datastore:无效的实体类型
最后我求助于 json。 contact 的切片被转换为 json 数组。
func (pb *Phonebook) Save() ([]datastore.Property, error) {
ps := []datastore.Property{
{
Name: "Title",
Value: pb.Title,
NoIndex: true,
},
}
var values []byte
if len(pb.Contacts) > 0 {
js, err := json.Marshal(pb.Contacts)
if err != nil {
return nil, err
}
values = js
}
ps = append(ps, datastore.Property{
Name: "Contacts",
Value: values,
NoIndex: true,
})
return ps, nil
}
除了使用 json 之外,还有更好的方法吗?
解决方案
我找到了这个 document,它提到 src 必须是一个结构指针。
终于介绍完啦!小伙伴们,这篇关于《正确的结构切片保存方法到 Cloud Datastore(Firestores数据存储模式下)是什么?》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
Golang · Go问答 | 1小时前 | channel · goroutine · pprof · Go问答 · 线上排查 · pprof Go问答 Go channel阻塞 goroutine堆积 channel发送205 收藏
-
Golang · Go问答 | 2小时前 | 连接池 · database/sql · Go问答 · 线上排查 · DBStats · MaxOpenConns Go数据库连接池 DBStats Go问答 sql.DB WaitCount355 收藏
-
467 收藏
-
Golang · Go问答 | 3小时前 | EventSource · sse · Go问答 · http.Flusher · 实时推送 · EventSource FLUSH text/event-stream Go问答 http.Flusher Go SSE333 收藏
-
Golang · Go问答 | 3小时前 | 并发安全 · RWMutex · sync.Map · Go问答 · map并发 · RWMutex sync.Map Go问答 Go map并发读写 concurrent map writes102 收藏
-
166 收藏
-
Golang · Go问答 | 5天前 | 并发 · channel · select · 性能排查 · Go问答 · select Go channel context default CPU飙高 忙等循环 ticker438 收藏
-
Golang · Go问答 | 5天前 | pprof · trace · 性能排查 · Go问答 · 服务安全 · Go pprof 生产环境 trace 安全入口 net/http/pprof 性能排障349 收藏
-
Golang · Go问答 | 5天前 | channel · 并发编程 · Go问答 · 背压 · 容量规划 · Goroutine channel 缓冲区 背压 Go问答 buffered channel 并发容量377 收藏
-
Golang · Go问答 | 5天前 | interface · 单元测试 · 架构设计 · repository · Go问答 · 单元测试 架构设计 interface 接口设计 Go问答 调用方定义 Repository212 收藏
-
Golang · Go问答 | 5天前 | JSON · time.Time · 接口设计 · Go问答 · encoding/json · encoding/json API响应 JSON序列化 time.Time omitempty Go问答 omitzero315 收藏
-
Golang · Go问答 | 5天前 | HTTP · Cookie · 浏览器 · cors · Go问答 · SameSite · cookie cors Secure SameSite Go问答 Set-Cookie 跨站请求 credentials246 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习