aboutsummaryrefslogtreecommitdiff
path: root/src/bytes/buffer.go
diff options
context:
space:
mode:
authorIlia Choly <ilia.choly@gmail.com>2025-10-31 20:11:04 +0000
committerGopher Robot <gobot@golang.org>2025-11-03 09:39:00 -0800
commit5132158ac2dee721790ad4b9f745bb6364406ea0 (patch)
treec57251c591ec8b1a095d91c5cbeb1078aa55b1f5 /src/bytes/buffer.go
parent361d51a6b58bccaab0559e06737c918018a7a5fa (diff)
downloadgo-5132158ac2dee721790ad4b9f745bb6364406ea0.tar.xz
bytes: add Buffer.Peek
Fixes #73794 Change-Id: I0a57db05aacfa805213fe8278fc727e76eb8a65e GitHub-Last-Rev: 3494d93f803f21905dfd5a9d593644da69279f16 GitHub-Pull-Request: golang/go#73795 Reviewed-on: https://go-review.googlesource.com/c/go/+/674415 Reviewed-by: Sean Liao <sean@liao.dev> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Michael Pratt <mpratt@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Pratt <mpratt@google.com>
Diffstat (limited to 'src/bytes/buffer.go')
-rw-r--r--src/bytes/buffer.go12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/bytes/buffer.go b/src/bytes/buffer.go
index 9684513942..3eb5b350c3 100644
--- a/src/bytes/buffer.go
+++ b/src/bytes/buffer.go
@@ -77,6 +77,18 @@ func (b *Buffer) String() string {
return string(b.buf[b.off:])
}
+// Peek returns the next n bytes without advancing the buffer.
+// If Peek returns fewer than n bytes, it also returns [io.EOF].
+// The slice is only valid until the next call to a read or write method.
+// The slice aliases the buffer content at least until the next buffer modification,
+// so immediate changes to the slice will affect the result of future reads.
+func (b *Buffer) Peek(n int) ([]byte, error) {
+ if b.Len() < n {
+ return b.buf[b.off:], io.EOF
+ }
+ return b.buf[b.off:n], nil
+}
+
// empty reports whether the unread portion of the buffer is empty.
func (b *Buffer) empty() bool { return len(b.buf) <= b.off }