aboutsummaryrefslogtreecommitdiff
path: root/src/bytes/bytes.go
diff options
context:
space:
mode:
authorMartin Möhrmann <martin@golang.org>2021-10-31 10:04:03 +0100
committerMartin Möhrmann <martin@golang.org>2022-08-15 19:17:20 +0000
commit7b45edb45016307151266731ccd158e14504598f (patch)
treeb70331b9b3088514973bac5ad65a15b8ee8a2b38 /src/bytes/bytes.go
parent69a8954282dc4500bb645e67fb912d53a7f78a5c (diff)
downloadgo-7b45edb45016307151266731ccd158e14504598f.tar.xz
bytes: add Clone function
The new Clone function returns a copy of b[:len(b)] for the input byte slice b. The result may have additional unused capacity. Clone(nil) returns nil. Fixes #45038 Change-Id: I0469a202d77a7b491f1341c08915d07ddd1f0300 Reviewed-on: https://go-review.googlesource.com/c/go/+/359675 Run-TryBot: Martin Möhrmann <martin@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Martin Möhrmann <moehrmann@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/bytes/bytes.go')
-rw-r--r--src/bytes/bytes.go10
1 files changed, 10 insertions, 0 deletions
diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go
index 659a82bcc8..27834fc6db 100644
--- a/src/bytes/bytes.go
+++ b/src/bytes/bytes.go
@@ -1299,3 +1299,13 @@ func Cut(s, sep []byte) (before, after []byte, found bool) {
}
return s, nil, false
}
+
+// Clone returns a copy of b[:len(b)].
+// The result may have additional unused capacity.
+// Clone(nil) returns nil.
+func Clone(b []byte) []byte {
+ if b == nil {
+ return nil
+ }
+ return append([]byte{}, b...)
+}