From 51d7faf078496ce2240a60217b31860134a5e411 Mon Sep 17 00:00:00 2001 From: Stephen Kitt Date: Tue, 16 Jun 2026 17:22:37 +0200 Subject: [PATCH] Provide a Go 1.26 new() version of ptr.To() This allows Go 1.26 codebases relying on ptr.To() to automatically migrate to new() using go fix. Signed-off-by: Stephen Kitt --- ptr/ptr.go | 5 ----- ptr/ptr_go125.go | 24 ++++++++++++++++++++++++ ptr/ptr_go126.go | 27 +++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 ptr/ptr_go125.go create mode 100644 ptr/ptr_go126.go diff --git a/ptr/ptr.go b/ptr/ptr.go index ea847fd3..0388eb21 100644 --- a/ptr/ptr.go +++ b/ptr/ptr.go @@ -46,11 +46,6 @@ func AllPtrFieldsNil(obj interface{}) bool { return true } -// To returns a pointer to the given value. -func To[T any](v T) *T { - return &v -} - // Deref dereferences ptr and returns the value it points to if not nil, or else // returns def. func Deref[T any](ptr *T, def T) T { diff --git a/ptr/ptr_go125.go b/ptr/ptr_go125.go new file mode 100644 index 00000000..e98243ed --- /dev/null +++ b/ptr/ptr_go125.go @@ -0,0 +1,24 @@ +//go:build !go1.26 + +/* +Copyright The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package ptr + +// To returns a pointer to the given value. +func To[T any](v T) *T { + return &v +} diff --git a/ptr/ptr_go126.go b/ptr/ptr_go126.go new file mode 100644 index 00000000..26d5eddb --- /dev/null +++ b/ptr/ptr_go126.go @@ -0,0 +1,27 @@ +//go:build go1.26 + +/* +Copyright The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package ptr + +// To returns a pointer to the given value. +// Deprecated: Use new(T) instead. +// +//go:fix inline +func To[T any](v T) *T { + return new(v) +}