Go 1.11 telah mencapai akhir dukungan
dan akan dihentikan penggunaannya
pada 31 Januari 2026. Setelah penghentian penggunaan, Anda tidak akan dapat men-deploy aplikasi Go 1.11, meskipun organisasi Anda sebelumnya menggunakan kebijakan organisasi untuk mengaktifkan kembali deployment runtime lama. Aplikasi Go 1.11 yang ada akan terus berjalan dan menerima traffic setelah tanggal penghentiannya. Sebaiknya Anda bermigrasi ke Go versi terbaru yang didukung.
Karena ID adalah bagian dari kunci entity, ID-nya terkait
secara permanen dengan entity dan tidak dapat diubah. Anda menetapkan ID ini
dengan salah satu dari dua cara berikut:
Tentukan string key name Anda sendiri untuk entity.
Biarkan Datastore secara otomatis menetapkan ID numerik bilangan bulat ke entity.
Menentukan nama kunci untuk entitas
Untuk menetapkan nama kunci ke entity, berikan argumen stringID yang tidak kosong ke
datastore.NewKey:
// 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)
Agar Datastore menetapkan ID numerik secara otomatis, gunakan argumen stringID kosong:
// Create a key such as Employee:8261.key:=datastore.NewKey(ctx,"Employee","",0,nil)// This is equivalent:key=datastore.NewIncompleteKey(ctx,"Employee",nil)
Kebijakan default menghasilkan urutan acak dari ID yang tidak digunakan yang kira-kira didistribusikan secara seragam. Setiap ID dapat memiliki maksimal 16 digit desimal.
Kebijakan legacy membuat urutan ID bilangan bulat yang lebih kecil dan tidak berurutan.
Jika Anda ingin menampilkan ID entity kepada pengguna, dan/atau bergantung pada urutannya, sebaiknya Anda menggunakan alokasi manual.
Menggunakan jalur ancestor
Entity dalam Cloud Datastore membentuk ruang terstruktur secara hierarkis yang mirip dengan
struktur direktori sistem file. Saat membuat entity, Anda dapat memilih untuk menetapkan entity lain sebagai parent; entity barunya adalah turunan dari parent entity tersebut (perlu diperhatikan bahwa tidak seperti dalam sistem file, parent entity tidak harus benar-benar ada). Entity tanpa parent adalah root entity. Kaitan entity dan induknya bersifat permanen, dan
tidak dapat diubah setelah entity tersebut dibuat. Cloud Datastore tidak akan pernah menetapkan ID numerik yang sama ke dua entity dengan parent yang sama, atau ke dua root entity (yang tidak memiliki parent).
Induk, induk dari induk, dan seterusnya dari suatu entity secara rekursif adalah
ancestor-nya; turunannya, turunan dari turunannya, dan seterusnya adalah turunannya. Root entity dan semua turunannya termasuk dalam entity group yang sama. Urutan entity yang dimulai dengan root entity dan berlanjut dari parent ke turunan, yang mengarah ke entity tertentu, merupakan jalur ancestor entity tersebut. Kunci lengkap yang mengidentifikasi
entity terdiri dari urutan pasangan jenis-ID yang menentukan
jalur ancestor-nya dan berakhir dengan entity itu sendiri:
Untuk root entity, jalur ancestor kosong dan kuncinya hanya terdiri dari jenis dan ID entity itu sendiri:
[Person:GreatGrandpa]
Konsep ini diilustrasikan oleh diagram berikut:
Untuk menetapkan parent entity, gunakan argumen parent ke
datastore.NewKey. Nilai
argumen ini harus berupa kunci entity parent.. Contoh berikut
membuat entity jenis Address dan menetapkan entity Employee sebagai
induknya:
// 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)
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-04 UTC."],[[["\u003cp\u003eThis 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.\u003c/p\u003e\n"],["\u003cp\u003eEach 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.\u003c/p\u003e\n"],["\u003cp\u003eIdentifiers for entities can be assigned either by specifying a key name string or by allowing Datastore to automatically assign an integer numeric ID.\u003c/p\u003e\n"],["\u003cp\u003eDatastore 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.\u003c/p\u003e\n"],["\u003cp\u003eEntities 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.\u003c/p\u003e\n"]]],[],null,["# Creating and Using Entity Keys\n\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| go\n| /services/access). If you are updating to the App Engine Go 1.12+ runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/go-differences) to learn about your migration options for legacy bundled services.\n\n\u003cbr /\u003e\n\nEach entity in Datastore has a *key* that uniquely identifies it. The key consists of the following components:\n\n- The *namespace* of the entity, which allows for [multitenancy](/appengine/docs/legacy/standard/go111/multitenancy)\n- The [*kind*](#Kinds_and_identifiers) of the entity, which categorizes it for the purpose of Datastore queries\n- An optional [*ancestor path*](#Ancestor_paths) locating the entity within the Datastore hierarchy.\n- An [*identifier*](#Kinds_and_identifiers) for the individual entity, which can be either\n\n - a *key name* string\n - an integer *numeric ID*\n\nBecause the identifier is part of the entity's key, the identifier is associated\npermanently with the entity and cannot be changed. You assign the identifier in\neither of two ways:\n\n- Specify your own *key name* string for the entity.\n- Let Datastore automatically assign the entity an integer *numeric ID*.\n\n| Don't name a property \"key.\" This name is reserved for a special property used to store the Model key. Though it may work locally, a property named \"key\" will prevent deployment to App Engine.\n\nSpecifying a key name for an entity\n-----------------------------------\n\nTo assign a key name to an entity, provide a non-empty `stringID` argument to\n[`datastore.NewKey`](/appengine/docs/legacy/standard/go/datastore/reference#NewKey): \n\n // Create a key with a key name \"asalieri\".\n key := datastore.NewKey(\n \tctx, // context.Context\n \t\"Employee\", // Kind\n \t\"asalieri\", // String ID; empty means no string ID\n \t0, // Integer ID; if 0, generate automatically. Ignored if string ID specified.\n \tnil, // Parent Key; nil means no parent\n )\n\nTo let Datastore assign a numeric ID automatically, use an empty `stringID` argument: \n\n // Create a key such as Employee:8261.\n key := datastore.NewKey(ctx, \"Employee\", \"\", 0, nil)\n // This is equivalent:\n key = datastore.NewIncompleteKey(ctx, \"Employee\", nil)\n\n### Assigning identifiers\n\nYou can configure Datastore to generate auto IDs using [two different auto id policies](/appengine/docs/legacy/standard/go111/config/appref#app_yaml_Auto_ID_policy):\n\n- The `default` policy generates a random sequence of unused IDs that are approximately uniformly distributed. Each ID can be up to 16 decimal digits long.\n- The `legacy` policy creates a sequence of non-consecutive smaller integer IDs.\n\nIf you want to display the entity IDs to the user, and/or depend upon their order, the best thing to do is use manual allocation.\n| **Note:** Instead of using key name strings or generating numeric IDs automatically, advanced applications may sometimes wish to assign their own numeric IDs manually to the entities they create. Be aware, however, that there is nothing to prevent Datastore from assigning one of your manual numeric IDs to another entity. The only way to avoid such conflicts is to have your application obtain a block of IDs with the [`datastore.AllocateIDs`](/appengine/docs/legacy/standard/go/datastore/reference#AllocateIDs) function. Datastore's automatic ID generator will keep track of IDs that have been allocated with this function and will avoid reusing them for another entity, so you can safely use such IDs without conflict.\n\nUsing ancestor paths\n--------------------\n\nEntities in Cloud Datastore form a hierarchically structured space similar to\nthe directory structure of a file system. When you create an entity, you can\noptionally designate another entity as its *parent;* the new entity is a\n*child* of the parent entity (note that unlike in a file system, the parent\nentity need not actually exist). An entity without a parent is a *root\nentity.* The association between an entity and its parent is permanent, and\ncannot be changed once the entity is created. Cloud Datastore will never assign\nthe same numeric ID to two entities with the same parent, or to two root\nentities (those without a parent).\n\nAn entity's parent, parent's parent, and so on recursively, are its\n*ancestors;* its children, children's children, and so on, are its\n*descendants.* A root entity and all of its descendants belong to\nthe same *entity group.* The sequence of entities beginning with a root\nentity and proceeding from parent to child, leading to a given entity,\nconstitute that entity's *ancestor path.* The complete key identifying\nthe entity consists of a sequence of kind-identifier pairs specifying its\nancestor path and terminating with those of the entity itself: \n\n```\n[Person:GreatGrandpa, Person:Grandpa, Person:Dad, Person:Me]\n```\n\nFor a root entity, the ancestor path is empty and the key consists solely of\nthe entity's own kind and identifier: \n\n```\n[Person:GreatGrandpa]\n```\n\nThis concept is illustrated by the following diagram:\n\n\nTo designate an entity's parent, use the `parent` argument to\n[`datastore.NewKey`](/appengine/docs/legacy/standard/go/datastore/reference#NewKey). The value\nof this argument should be the parent entity's key.. The following example\ncreates an entity of kind `Address` and designates an `Employee` entity as its\nparent: \n\n // Create Employee entity\n employee := &Employee{ /* ... */ }\n employeeKey, err := datastore.Put(ctx, datastore.NewIncompleteKey(ctx, \"Employee\", nil), employee)\n\n // Use Employee as Address entity's parent\n // and save Address entity to datastore\n address := &Address{ /* ... */ }\n addressKey := datastore.NewIncompleteKey(ctx, \"Address\", employeeKey)\n _, err = datastore.Put(ctx, addressKey, address)"]]