最近发现不少小伙伴都对golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《将Golang 在嵌入式系统中的数据存储》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存
最近发现不少小伙伴都对golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《将Golang 在嵌入式系统中的数据存储》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~
问题内容如何使用 golang cloud.google.com
库将 embeddedentity
写入数据存储?
我在一家公司工作,我们正在从 GCp java 迁移到 golang。 java core 要求将 embeddedentity
s 写入数据存储区,以便 core 获取和读取有关用户的某些信息。
我们正在迁移到 golang,并要求 embeddedentity
从 golang 编写。我尝试将 struct
编组为 JSON
,并直接写入。但是,它仍然保留为 string
类型。目前,更改 java core 中的生产代码对我们来说不是一个可行的选择。
// VBalEmbeddedEntity is a Struct representation of a jsON entity
type VBalEmbeddedEntity struct {
Properties Properties `json:"properties"`
}
type Properties struct {
VT1_1 IntegerValue `json:"VT1_1"`
}
type IntegerValue struct {
IntegerValue string `json:"integerValue"`
}
// Create EmbeddedEntity
bytes, err := json.Marshal(VBalEmbeddedEntity{
Properties: Properties{
VT1_1: IntegerValue{
IntegerValue: strconv.Itoa(int(record.Amount)),
},
},
})
if err != nil {
return 0, 0, err
}
// Generate Data using record
vbal := string(bytes)
f4Key := datastore.IncompleteKey("MyKind", nil)
f4E := F1_4{VBal: vbal}
// Datastore Put
d.DB.Put(ctx, f4Key, &f4E)
要使用 golang 将 embeddedentity
写入数据存储区,您必须执行以下操作:
type EmbeddedEntity struct {
VT1_1 int
}
type F1_4 struct {
VBal EmbeddedEntity
}
func run() error {
// Create EmbeddedEntity
embeddedEntity := EmbeddedEntity{
VT1_1: int(record.Amount),
}
// Generate Data containing EmbeddedEntity
f4E := F1_4{VBal: embeddedEntity}
// Put() to Datastore. VBal Field will be encoded as EmbeddedEntity type
err := d.DB.Put(ctx, datastore.IncompleteKey("MyKind", nil), &f4E)
if err != nil {
return err
}
}
感谢@JimMorrison为我指明了正确的方向! :)
到这里,我们也就讲完了《将Golang 在嵌入式系统中的数据存储》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注编程网公众号,带你了解更多关于的知识点!
--结束END--
本文标题: 将Golang 在嵌入式系统中的数据存储
本文链接: https://www.lsjlt.com/news/595693.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-05
2024-04-05
2024-04-05
2024-04-04
2024-04-05
2024-04-05
2024-04-05
2024-04-05
2024-04-04
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0