google-app-engine - golang 数据存储结构 : keeping field unique and required

标签 google-app-engine go struct google-cloud-datastore

我想知道如何最好地保证一个字段是唯一的,如果不是,则不会保存到数据存储中。另外,它应该是必需的。我将此字段用作 stringID 并需要它是唯一的。我知道我可以简单地尝试通过该字段获取实体并查看它是否存在并围绕它构建逻辑。但是有没有更简单的方法,比如在您的结构中声明该字段应该是唯一的和/或必需的?就像下面的模型。

type Car struct {
    Regnr  string "required" "unique"
}

谢谢!

最佳答案

来自数据存储 API:

By default, for struct pointers, all properties are potentially indexed, and the property name is the same as the field name (and hence must start with an upper case letter). Fields may have a datastore:"name,options" tag. The tag name is the property name, which must be one or more valid Go identifiers joined by ".", but may start with a lower case letter. An empty tag name means to just use the field name. A "-" tag name means that the datastore will ignore that field. If options is "noindex" then the field will not be indexed. If the options is "" then the comma may be omitted. There are no other recognized options.

无法使用 Datastore 设置这些类型的标签。

关于google-app-engine - golang 数据存储结构 : keeping field unique and required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34575542/

相关文章:

for-loop - 从 3 个单独的 slice (每个结构属性一个 slice )创建新的组合结构 slice 时索引超出范围

google-app-engine - 从 Google App Engine 下载服务器错误日志

java - 更新到 java 6u31 后 DevServer 失败

C - 大端结构与小端结构相互转换

go - 是否可以同时计算文件的md5并将文件写入磁盘?

go - 为什么 Go channel 会限制缓冲区大小

c++ - 用 sockaddr_in 的值填充 sockaddr_storage 结构

python - 合并缓存的 GQL 查询而不是使用 IN

java - Endpoints Frameworks 2.0 迁移后 Endpoint Api 调用错误

go - 来自 GOPL 的代码 : concurrent web crawler