From aa844e102ed7f8a3e03f45acb75fe7f341d814be Mon Sep 17 00:00:00 2001 From: hopehook Date: Sat, 1 Oct 2022 21:37:46 +0800 Subject: [PATCH 1/4] Feat: Convert strings and slices using the officially recommended way. Go official is expected to provide unsafe.{SliceData, Slice, StringData, String} series methods in version 1.20 for conversion of strings and slices. --- go.mod | 2 +- .../{bytesconv.go => bytesconv_1.19.go} | 3 +++ internal/bytesconv/bytesconv_1.20.go | 22 +++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) rename internal/bytesconv/{bytesconv.go => bytesconv_1.19.go} (93%) create mode 100644 internal/bytesconv/bytesconv_1.20.go diff --git a/go.mod b/go.mod index fa1db99ac4..0302b7d754 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/gin-gonic/gin -go 1.18 +go 1.20 require ( github.com/bytedance/sonic v1.4.0 diff --git a/internal/bytesconv/bytesconv.go b/internal/bytesconv/bytesconv_1.19.go similarity index 93% rename from internal/bytesconv/bytesconv.go rename to internal/bytesconv/bytesconv_1.19.go index 86e4c4d44c..15b613c629 100644 --- a/internal/bytesconv/bytesconv.go +++ b/internal/bytesconv/bytesconv_1.19.go @@ -2,6 +2,9 @@ // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. +//go:build !go1.20 +// +build !go1.20 + package bytesconv import ( diff --git a/internal/bytesconv/bytesconv_1.20.go b/internal/bytesconv/bytesconv_1.20.go new file mode 100644 index 0000000000..93879468a7 --- /dev/null +++ b/internal/bytesconv/bytesconv_1.20.go @@ -0,0 +1,22 @@ +// Copyright 2022 Gin Core Team. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +//go:build go1.20 +// +build go1.20 + +package bytesconv + +import ( + "unsafe" +) + +// StringToBytes converts string to byte slice without a memory allocation. +func StringToBytes(s string) []byte { + return unsafe.Slice(unsafe.StringData(s), len(s)) +} + +// BytesToString converts byte slice to string without a memory allocation. +func BytesToString(b []byte) string { + return unsafe.String(unsafe.SliceData(b), len(b)) +} From ad7779d934e177d4e94ce6c5f83075278450912a Mon Sep 17 00:00:00 2001 From: hopehook Date: Wed, 8 Feb 2023 17:40:59 +0800 Subject: [PATCH 2/4] chore: add reference documentation link to comment of code --- internal/bytesconv/bytesconv_1.20.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/bytesconv/bytesconv_1.20.go b/internal/bytesconv/bytesconv_1.20.go index 93879468a7..7c1cae1254 100644 --- a/internal/bytesconv/bytesconv_1.20.go +++ b/internal/bytesconv/bytesconv_1.20.go @@ -12,11 +12,13 @@ import ( ) // StringToBytes converts string to byte slice without a memory allocation. +// For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077. func StringToBytes(s string) []byte { return unsafe.Slice(unsafe.StringData(s), len(s)) } // BytesToString converts byte slice to string without a memory allocation. +// For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077. func BytesToString(b []byte) string { return unsafe.String(unsafe.SliceData(b), len(b)) } From f684de861824ff54e91b5b873403dcb067f6c567 Mon Sep 17 00:00:00 2001 From: hopehook Date: Wed, 8 Feb 2023 23:23:26 +0800 Subject: [PATCH 3/4] chore: update Copyright --- internal/bytesconv/bytesconv_1.20.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/bytesconv/bytesconv_1.20.go b/internal/bytesconv/bytesconv_1.20.go index 7c1cae1254..f5001878d5 100644 --- a/internal/bytesconv/bytesconv_1.20.go +++ b/internal/bytesconv/bytesconv_1.20.go @@ -1,4 +1,4 @@ -// Copyright 2022 Gin Core Team. All rights reserved. +// Copyright 2023 Gin Core Team. All rights reserved. // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. From 76a8ac817897c0a7d221fd8faa60ade3aec93ff9 Mon Sep 17 00:00:00 2001 From: hopehook Date: Wed, 1 Mar 2023 10:22:15 +0800 Subject: [PATCH 4/4] chore: remove build tag "+build !go1.20" --- internal/bytesconv/bytesconv_1.19.go | 1 - internal/bytesconv/bytesconv_1.20.go | 1 - 2 files changed, 2 deletions(-) diff --git a/internal/bytesconv/bytesconv_1.19.go b/internal/bytesconv/bytesconv_1.19.go index 15b613c629..669c9c914e 100644 --- a/internal/bytesconv/bytesconv_1.19.go +++ b/internal/bytesconv/bytesconv_1.19.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build !go1.20 -// +build !go1.20 package bytesconv diff --git a/internal/bytesconv/bytesconv_1.20.go b/internal/bytesconv/bytesconv_1.20.go index f5001878d5..5b6040a6b3 100644 --- a/internal/bytesconv/bytesconv_1.20.go +++ b/internal/bytesconv/bytesconv_1.20.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build go1.20 -// +build go1.20 package bytesconv