Skip to content

datastore: modernize codebase using Go 1.21–1.26 standard library idioms #20666

Closed

Description

@bhshkh

Description

Now that cloud.google.com/go/datastore targets Go 1.26+, several legacy patterns across the module can be updated to use modern Go 1.21–1.26 standard library APIs for cleaner code and fewer heap allocations.

Scope of Modernization

  1. Automated modernize pass (golang.org/x/tools/gopls/internal/analysis/modernize):

    • Replace reflect.TypeOf(...) initializers in load.go, prop.go, and tests with reflect.TypeFor[T]() (Go 1.22).
    • Replace for i := 0; i < t.NumField(); i++ in prop.go (validateChildType) with for f := range t.Fields() (Go 1.26).
    • Replace for _, s := range strings.Split(name, ".") in prop.go (validPropertyName) with zero-allocation strings.SplitSeq(name, ".") (Go 1.24).
    • Replace for i := 0; i < n; i++ loops in transaction.go and tests with for range n (Go 1.22), and use min built-in where applicable (Go 1.21).
  2. Follow-up Hot-Path String & Slice Modernizations:

    • Replace bytes.Buffer with strings.Builder in Key.marshal / Key.string (key.go), which is called on every key during Client.GetMulti and Client.DeleteMulti deduplication, eliminating the extra []byte -> string copy allocation.
    • Replace append([]string(nil), fieldNames...) in Query.Project (query.go) with slices.Clone.

Metadata

Metadata

Assignees

Labels

api: datastoreIssues related to the Datastore API.type: cleanupAn internal cleanup or hygiene concern.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions