// Create a key with a key name "asalieri".key:=datastore.NewKey(ctx,// context.Context"Employee",// Kind"asalieri",// String ID; empty means no string ID0,// Integer ID; if 0, generate automatically. Ignored if string ID specified.nil,// Parent Key; nil means no parent)
如需让 Datastore 自动分配数字 ID,请使用空的 stringID 参数:
// Create a key such as Employee:8261.key:=datastore.NewKey(ctx,"Employee","",0,nil)// This is equivalent:key=datastore.NewIncompleteKey(ctx,"Employee",nil)
// Create Employee entityemployee:=&Employee{/* ... */}employeeKey,err:=datastore.Put(ctx,datastore.NewIncompleteKey(ctx,"Employee",nil),employee)// Use Employee as Address entity's parent// and save Address entity to datastoreaddress:=&Address{/* ... */}addressKey:=datastore.NewIncompleteKey(ctx,"Address",employeeKey)_,err=datastore.Put(ctx,addressKey,address)
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-03-07。"],[[["This API supports first-generation runtimes and can be used when upgrading to corresponding second-generation runtimes, with a migration guide available for those updating to the App Engine Go 1.12+ runtime."],["Each entity in Datastore is uniquely identified by a key, which includes the entity's namespace, kind, an optional ancestor path, and an identifier that can either be a key name string or an integer numeric ID."],["Identifiers for entities can be assigned either by specifying a key name string or by allowing Datastore to automatically assign an integer numeric ID."],["Datastore offers two auto ID policies, 'default' and 'legacy', to generate automatic IDs, and manual allocation is the preferred method for applications that need to display or depend on entity ID order."],["Entities in Cloud Datastore can form a hierarchy with parent-child relationships, where the ancestor path defines the lineage of an entity from a root entity, and the relationship between an entity and its parent is permanent."]]],[]]