登录
首页 >  Golang >  Go问答

将Golang 在嵌入式系统中的数据存储

来源:stackoverflow

时间:2024-03-28 08:36:28 162浏览 收藏

最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《将Golang 在嵌入式系统中的数据存储》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~

问题内容

如何使用 golang cloud.google.com 库将 embeddedentity 写入数据存储?

我在一家公司工作,我们正在从 gcp java 迁移到 golang。 java core 要求将 embeddedentitys 写入数据存储区,以便 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 在嵌入式系统中的数据存储》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>