diff --git a/util/util_unsafe.go b/util/util_unsafe_go120.go similarity index 87% rename from util/util_unsafe.go rename to util/util_unsafe_go120.go index 840007a..d6be534 100644 --- a/util/util_unsafe.go +++ b/util/util_unsafe_go120.go @@ -1,5 +1,5 @@ -//go:build !appengine && !js -// +build !appengine,!js +//go:build !appengine && !js && !go1.21 +// +build !appengine,!js,!go1.21 package util diff --git a/util/util_unsafe_go121.go b/util/util_unsafe_go121.go new file mode 100644 index 0000000..50c7fce --- /dev/null +++ b/util/util_unsafe_go121.go @@ -0,0 +1,18 @@ +//go:build !appengine && !js && go1.21 +// +build !appengine,!js,go1.21 + +package util + +import ( + "unsafe" +) + +// BytesToReadOnlyString returns a string converted from given bytes. +func BytesToReadOnlyString(b []byte) string { + return unsafe.String(unsafe.SliceData(b), len(b)) +} + +// StringToReadOnlyBytes returns bytes converted from given string. +func StringToReadOnlyBytes(s string) []byte { + return unsafe.Slice(unsafe.StringData(s), len(s)) +}