Overview
Index
- Variables
- func Contains(b, subslice []byte) bool
- func ContainsRune(b []byte, r rune) bool
- func Equal(a, b []byte) bool
- func Fields(s []byte) [][]byte
- func HasPrefix(s, prefix []byte) bool
- func Index(s, sep []byte) int
- func IndexByte(s []byte, c byte) int
- func IndexRune(s []byte, r rune) int
- func LastIndex(s, sep []byte) int
- func LastIndexByte(s []byte, c byte) int
- func Map(mapping func(r rune) rune, s []byte) []byte
- func Replace(s, old, new []byte, n int) []byte
- func Split(s, sep []byte) [][]byte
- func SplitAfterN(s, sep []byte, n int) [][]byte
- func Title(s []byte) []byte
- func ToLowerSpecial(c unicode.SpecialCase, s []byte) []byte
- func ToTitleSpecial(c unicode.SpecialCase, s []byte) []byte
- func ToUpperSpecial(c unicode.SpecialCase, s []byte) []byte
- func TrimFunc(s []byte, f func(r rune) bool) []byte
- func TrimLeftFunc(s []byte, f func(r rune) bool) []byte
- func TrimRight(s []byte, cutset string) []byte
- func TrimSpace(s []byte) []byte
- type Buffer
- func NewBufferString(s string) *Buffer
- func (b *Buffer) Cap() int
- func (b *Buffer) Len() int
- func (b *Buffer) Read(p []byte) (n int, err error)
- func (b *Buffer) ReadBytes(delim byte) (line []byte, err error)
- func (b *Buffer) ReadRune() (r rune, size int, err error)
- func (b *Buffer) Reset()
- func (b *Buffer) Truncate(n int)
- func (b *Buffer) UnreadRune() error
- func (b *Buffer) WriteByte(c byte) error
- func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)
- Bugs
- Buffer.Grow
- Compare
- Contains
- ContainsRune
- Equal
- Fields
- HasPrefix
- Index
- IndexByte
- IndexRune
- LastIndex
- LastIndexByte
- Map
- Repeat
- Runes
- SplitAfter
- SplitN
- ToLower
- ToUpper
- TrimFunc
- TrimLeftFunc
- TrimRight
- TrimSpace
Package files
Constants
Variables
- var ErrTooLarge = errors.("bytes.Buffer: too large")
ErrTooLarge is passed to panic if memory cannot be allocated to store data in a
buffer.
func Compare
- func Compare(a, b []byte)
Compare returns an integer comparing two byte slices lexicographically. The
result will be 0 if a==b, -1 if a < b, and +1 if a > b. A nil argument is
equivalent to an empty slice.
Example:
// Interpret Compare’s result by comparing it to zero.
var a, b []byte
if bytes.Compare(a, b) < 0 {
// a less b
}
if bytes.Compare(a, b) <= 0 {
// a less or equal b
}
if bytes.Compare(a, b) > 0 {
// a greater b
}
if bytes.Compare(a, b) >= 0 {
// a greater or equal b
}
// Prefer Equal to Compare for equality comparisons.
if bytes.Equal(a, b) {
// a equal b
}
if !bytes.Equal(a, b) {
// a not equal b
}
Example:
// Binary search to find a matching byte slice.
var needle []byte
var haystack [][]byte // Assume sorted
i := sort.Search(len(haystack), func(i int) bool {
// Return haystack[i] >= needle.
return bytes.Compare(haystack[i], needle) >= 0
})
if i < len(haystack) && bytes.Equal(haystack[i], needle) {
// Found it!
}
func Contains
- func Contains(b, subslice []byte)
Contains reports whether subslice is within b.
Example:
fmt.Println(bytes.Contains([]byte(“seafood”), []byte(“foo”)))
fmt.Println(bytes.Contains([]byte(“seafood”), []byte(“bar”)))
fmt.Println(bytes.Contains([]byte(“seafood”), []byte(“”)))
fmt.Println(bytes.Contains([]byte(“”), []byte(“”)))
// Output:
// true
// false
// true
// true
func
¶
- func ContainsAny(b [], chars string)
ContainsAny reports whether any of the UTF-8-encoded code points in chars are
within b.
Example:
fmt.Println(bytes.ContainsAny([]byte(“I like seafood.”), “fÄo!”))
fmt.Println(bytes.ContainsAny([]byte(“I like seafood.”), “去是伟大的.”))
fmt.Println(bytes.ContainsAny([]byte(“I like seafood.”), “”))
fmt.Println(bytes.ContainsAny([]byte(“”), “”))
// Output:
// true
// true
// false
// false
func
¶
- func ContainsRune(b [], r rune)
ContainsRune reports whether the rune is contained in the UTF-8-encoded byte
slice b.
Example:
fmt.Println(bytes.ContainsRune([]byte(“I like seafood.”), ‘f’))
fmt.Println(bytes.ContainsRune([]byte(“I like seafood.”), ‘ö’))
fmt.Println(bytes.ContainsRune([]byte(“去是伟大的!”), ‘大’))
fmt.Println(bytes.ContainsRune([]byte(“去是伟大的!”), ‘!’))
fmt.Println(bytes.ContainsRune([]byte(“”), ‘@’))
// Output:
// true
// false
// true
// true
// false
func
¶
- func Count(s, sep []) int
Count counts the number of non-overlapping instances of sep in s. If sep is an
empty slice, Count returns 1 + the number of UTF-8-encoded code points in s.
Example:
fmt.Println(bytes.Count([]byte(“cheese”), []byte(“e”)))
fmt.Println(bytes.Count([]byte(“five”), []byte(“”))) // before & after each rune
// Output:
// 3
// 5
func Equal
- func Equal(a, b []byte)
Equal returns a boolean reporting whether a and b are the same length and
contain the same bytes. A nil argument is equivalent to an empty slice.
Example:
fmt.Println(bytes.Equal([]byte(“Go”), []byte(“Go”)))
fmt.Println(bytes.Equal([]byte(“Go”), []byte(“C++”)))
// Output:
// true
// false
func
¶
- func EqualFold(s, t []) bool
EqualFold reports whether s and t, interpreted as UTF-8 strings, are equal under
Unicode case-folding.
Example:
fmt.Println(bytes.EqualFold([]byte(“Go”), []byte(“go”)))
// Output: true
func Fields
- func Fields(s []byte) [][]
Fields interprets s as a sequence of UTF-8-encoded code points. It splits the
slice s around each instance of one or more consecutive white space characters,
as defined by unicode.IsSpace, returning a slice of subslices of s or an empty
slice if s contains only white space.
Example:
fmt.Printf(“Fields are: %q”, bytes.Fields([]byte(“ foo bar baz “)))
// Output: Fields are: [“foo” “bar” “baz”]
func
¶
FieldsFunc interprets s as a sequence of UTF-8-encoded code points. It splits
the slice s at each run of code points c satisfying f(c) and returns a slice of
subslices of s. If all code points in s satisfy f(c), or len(s) == 0, an empty
slice is returned. FieldsFunc makes no guarantees about the order in which it
calls f(c). If f does not return consistent results for a given c, FieldsFunc
may crash.
Example:
f := func(c rune) bool {
return !unicode.IsLetter(c) && !unicode.IsNumber(c)
}
fmt.Printf(“Fields are: %q”, bytes.FieldsFunc([]byte(“ foo1;bar2,baz3…”), f))
// Output: Fields are: [“foo1” “bar2” “baz3”]
func HasPrefix
- func HasPrefix(s, prefix []byte)
HasPrefix tests whether the byte slice s begins with prefix.
Example:
fmt.Println(bytes.HasPrefix([]byte(“Gopher”), []byte(“Go”)))
fmt.Println(bytes.HasPrefix([]byte(“Gopher”), []byte(“C”)))
fmt.Println(bytes.HasPrefix([]byte(“Gopher”), []byte(“”)))
// Output:
// true
// false
// true
func
¶
- func HasSuffix(s, suffix []) bool
HasSuffix tests whether the byte slice s ends with suffix.
Example:
fmt.Println(bytes.HasSuffix([]byte(“Amigo”), []byte(“go”)))
fmt.Println(bytes.HasSuffix([]byte(“Amigo”), []byte(“O”)))
fmt.Println(bytes.HasSuffix([]byte(“Amigo”), []byte(“Ami”)))
fmt.Println(bytes.HasSuffix([]byte(“Amigo”), []byte(“”)))
// Output:
// true
// false
// false
// true
func Index
- func Index(s, sep []byte)
Index returns the index of the first instance of sep in s, or -1 if sep is not
present in s.
Example:
fmt.Println(bytes.Index([]byte(“chicken”), []byte(“ken”)))
fmt.Println(bytes.Index([]byte(“chicken”), []byte(“dmr”)))
// Output:
// 4
// -1
- func IndexAny(s [], chars string)
IndexAny interprets s as a sequence of UTF-8-encoded Unicode code points. It
returns the byte index of the first occurrence in s of any of the Unicode code
points in chars. It returns -1 if chars is empty or if there is no code point in
common.
Example:
fmt.Println(bytes.IndexAny([]byte(“chicken”), “aeiouy”))
fmt.Println(bytes.IndexAny([]byte(“crwth”), “aeiouy”))
// Output:
// 2
// -1
func
¶
- func IndexByte(s [], c byte)
IndexByte returns the index of the first instance of c in s, or -1 if c is not
present in s.
Example:
fmt.Println(bytes.IndexByte([]byte(“chicken”), byte(‘k’)))
fmt.Println(bytes.IndexByte([]byte(“chicken”), byte(‘g’)))
// Output:
// 4
// -1
func
¶
IndexFunc interprets s as a sequence of UTF-8-encoded code points. It returns
the byte index in s of the first Unicode code point satisfying f(c), or -1 if
none do.
Example:
f := func(c rune) bool {
return unicode.Is(unicode.Han, c)
}
fmt.Println(bytes.IndexFunc([]byte(“Hello, 世界”), f))
fmt.Println(bytes.IndexFunc([]byte(“Hello, world”), f))
// Output:
// 7
// -1
func IndexRune
IndexRune interprets s as a sequence of UTF-8-encoded code points. It returns
the byte index of the first occurrence in s of the given rune. It returns -1 if
rune is not present in s. If r is utf8.RuneError, it returns the first instance
of any invalid UTF-8 byte sequence.
Example:
fmt.Println(bytes.IndexRune([]byte(“chicken”), ‘k’))
fmt.Println(bytes.IndexRune([]byte(“chicken”), ‘d’))
// Output:
// 4
// -1
func Join
Join concatenates the elements of s to create a new byte slice. The separator
sep is placed between elements in the resulting slice.
Example:
s := [][]byte{[]byte(“foo”), []byte(“bar”), []byte(“baz”)}
fmt.Printf(“%s”, bytes.Join(s, []byte(“, “)))
// Output: foo, bar, baz
func LastIndex
- func LastIndex(s, sep []byte)
LastIndex returns the index of the last instance of sep in s, or -1 if sep is
not present in s.
Example:
fmt.Println(bytes.Index([]byte(“go gopher”), []byte(“go”)))
fmt.Println(bytes.LastIndex([]byte(“go gopher”), []byte(“go”)))
fmt.Println(bytes.LastIndex([]byte(“go gopher”), []byte(“rodent”)))
// Output:
// 0
// 3
// -1
func
¶
- func LastIndexAny(s [], chars string)
LastIndexAny interprets s as a sequence of UTF-8-encoded Unicode code points. It
returns the byte index of the last occurrence in s of any of the Unicode code
points in chars. It returns -1 if chars is empty or if there is no code point in
common.
Example:
fmt.Println(bytes.LastIndexAny([]byte(“go gopher”), “MüQp”))
fmt.Println(bytes.LastIndexAny([]byte(“go 地鼠”), “地大”))
fmt.Println(bytes.LastIndexAny([]byte(“go gopher”), “z,!.”))
// Output:
// 5
// 3
// -1
func
¶
- func LastIndexByte(s [], c byte)
LastIndexByte returns the index of the last instance of c in s, or -1 if c is
not present in s.
Example:
fmt.Println(bytes.LastIndexByte([]byte(“go gopher”), byte(‘g’)))
fmt.Println(bytes.LastIndexByte([]byte(“go gopher”), byte(‘r’)))
fmt.Println(bytes.LastIndexByte([]byte(“go gopher”), byte(‘z’)))
// Output:
// 3
// 8
// -1
func
¶
LastIndexFunc interprets s as a sequence of UTF-8-encoded code points. It
returns the byte index in s of the last Unicode code point satisfying f(c), or
-1 if none do.
Example:
fmt.Println(bytes.LastIndexFunc([]byte(“go gopher!”), unicode.IsLetter))
fmt.Println(bytes.LastIndexFunc([]byte(“go gopher!”), unicode.IsPunct))
fmt.Println(bytes.LastIndexFunc([]byte(“go gopher!”), unicode.IsNumber))
// Output:
// 8
// 9
// -1
func Map
Map returns a copy of the byte slice s with all its characters modified
according to the mapping function. If mapping returns a negative value, the
character is dropped from the byte slice with no replacement. The characters in
s and the output are interpreted as UTF-8-encoded code points.
Example:
rot13 := func(r rune) rune {
switch {
case r >= ‘A’ && r <= ‘Z’:
return ‘A’ + (r-‘A’+13)%26
case r >= ‘a’ && r <= ‘z’:
return ‘a’ + (r-‘a’+13)%26
}
return r
}
fmt.Printf(“%s”, bytes.Map(rot13, []byte(“‘Twas brillig and the slithy gopher…”)))
// Output: ‘Gjnf oevyyvt naq gur fyvgul tbcure…
func
¶
- func Repeat(b [], count int) []
Repeat returns a new byte slice consisting of count copies of b.
It panics if count is negative or if the result of (len(b) count) overflows.
Example:
fmt.Printf(“ba%s”, bytes.Repeat([]byte(“na”), 2))
// Output: banana
func
¶
- func Replace(s, old, new [], n int) []
Replace returns a copy of the slice s with the first n non-overlapping instances
of old replaced by new. If old is empty, it matches at the beginning of the
slice and after each UTF-8 sequence, yielding up to k+1 replacements for a
k-rune slice. If n < 0, there is no limit on the number of replacements.
Example:
fmt.Printf(“%s\n”, bytes.Replace([]byte(“oink oink oink”), []byte(“k”), []byte(“ky”), 2))
fmt.Printf(“%s\n”, bytes.Replace([]byte(“oink oink oink”), []byte(“oink”), []byte(“moo”), -1))
// Output:
// oinky oinky oink
// moo moo moo
func
¶
- func Runes(s []) []rune
Runes interprets s as a sequence of UTF-8-encoded code points. It returns a
slice of runes (Unicode code points) equivalent to s.
Example:
rs := bytes.Runes([]byte(“go gopher”))
for _, r := range rs {
fmt.Printf(“%#U\n”, r)
}
// Output:
// U+0067 ‘g’
// U+006F ‘o’
// U+0020 ‘ ‘
// U+0067 ‘g’
// U+006F ‘o’
// U+0070 ‘p’
// U+0068 ‘h’
// U+0065 ‘e’
// U+0072 ‘r’
func Split
- func Split(s, sep []byte) [][]
Split slices s into all subslices separated by sep and returns a slice of the
subslices between those separators. If sep is empty, Split splits after each
UTF-8 sequence. It is equivalent to SplitN with a count of -1.
Example:
fmt.Printf(“%q\n”, bytes.Split([]byte(“a,b,c”), []byte(“,”)))
fmt.Printf(“%q\n”, bytes.Split([]byte(“a man a plan a canal panama”), []byte(“a “)))
fmt.Printf(“%q\n”, bytes.Split([]byte(“ xyz “), []byte(“”)))
fmt.Printf(“%q\n”, bytes.Split([]byte(“”), []byte(“Bernardo O’Higgins”)))
// Output:
// [“a” “b” “c”]
// [“” “man “ “plan “ “canal panama”]
// [“ “ “x” “y” “z” “ “]
// [“”]
func
¶
SplitAfter slices s into all subslices after each instance of sep and returns a
slice of those subslices. If sep is empty, SplitAfter splits after each UTF-8
sequence. It is equivalent to SplitAfterN with a count of -1.
Example:
fmt.Printf(“%q\n”, bytes.SplitAfter([]byte(“a,b,c”), []byte(“,”)))
// Output: [“a,” “b,” “c”]
func SplitAfterN
SplitAfterN slices s into subslices after each instance of sep and returns a
slice of those subslices. If sep is empty, SplitAfterN splits after each UTF-8
sequence. The count determines the number of subslices to return:
n > 0: at most n subslices; the last subslice will be the unsplit remainder.
n == 0: the result is nil (zero subslices)
n < 0: all subslices
Example:
fmt.Printf(“%q\n”, bytes.SplitAfterN([]byte(“a,b,c”), []byte(“,”), 2))
// Output: [“a,” “b,c”]
func SplitN
SplitN slices s into subslices separated by sep and returns a slice of the
subslices between those separators. If sep is empty, SplitN splits after each
UTF-8 sequence. The count determines the number of subslices to return:
n > 0: at most n subslices; the last subslice will be the unsplit remainder.
n == 0: the result is nil (zero subslices)
n < 0: all subslices
Example:
fmt.Printf(“%q\n”, bytes.SplitN([]byte(“a,b,c”), []byte(“,”), 2))
z := bytes.SplitN([]byte(“a,b,c”), []byte(“,”), 0)
fmt.Printf(“%q (nil = %v)\n”, z, z == nil)
// Output:
// [“a” “b,c”]
// [] (nil = true)
- func Title(s []byte) []
Title treats s as UTF-8-encoded bytes and returns a copy with all Unicode
letters that begin words mapped to their title case.
BUG(rsc): The rule Title uses for word boundaries does not handle Unicode
punctuation properly.
Example:
fmt.Printf(“%s”, bytes.Title([]byte(“her royal highness”)))
// Output: Her Royal Highness
func
¶
- func ToLower(s []) []byte
ToLower treats s as UTF-8-encoded bytes and returns a copy with all the Unicode
letters mapped to their lower case.
Example:
fmt.Printf(“%s”, bytes.ToLower([]byte(“Gopher”)))
// Output: gopher
func ToLowerSpecial
ToLowerSpecial treats s as UTF-8-encoded bytes and returns a copy with all the
Unicode letters mapped to their lower case, giving priority to the special
casing rules.
func ToTitle
- func ToTitle(s []byte) []
ToTitle treats s as UTF-8-encoded bytes and returns a copy with all the Unicode
letters mapped to their title case.
Example:
fmt.Printf(“%s\n”, bytes.ToTitle([]byte(“loud noises”)))
fmt.Printf(“%s\n”, bytes.ToTitle([]byte(“хлеб”)))
// Output:
// LOUD NOISES
// ХЛЕБ
func
¶
- func ToTitleSpecial(c .SpecialCase, s []) []byte
ToTitleSpecial treats s as UTF-8-encoded bytes and returns a copy with all the
Unicode letters mapped to their title case, giving priority to the special
casing rules.
func
¶
- func ToUpper(s []) []byte
ToUpper treats s as UTF-8-encoded bytes and returns a copy with all the Unicode
letters within it mapped to their upper case.
Example:
fmt.Printf(“%s”, bytes.ToUpper([]byte(“Gopher”)))
// Output: GOPHER
func ToUpperSpecial
ToUpperSpecial treats s as UTF-8-encoded bytes and returns a copy with all the
Unicode letters mapped to their upper case, giving priority to the special
casing rules.
func Trim
Trim returns a subslice of s by slicing off all leading and trailing
UTF-8-encoded code points contained in cutset.
Example:
fmt.Printf(“[%q]”, bytes.Trim([]byte(“ !!! Achtung! Achtung! !!! “), “! “))
// Output: [“Achtung! Achtung”]
func TrimFunc
TrimFunc returns a subslice of s by slicing off all leading and trailing
UTF-8-encoded code points c that satisfy f(c).
Example:
fmt.Println(string(bytes.TrimFunc([]byte(“go-gopher!”), unicode.IsLetter)))
fmt.Println(string(bytes.TrimFunc([]byte(“\”go-gopher!\””), unicode.IsLetter)))
fmt.Println(string(bytes.TrimFunc([]byte(“go-gopher!”), unicode.IsPunct)))
fmt.Println(string(bytes.TrimFunc([]byte(“1234go-gopher!567”), unicode.IsNumber)))
// Output:
// -gopher!
// “go-gopher!”
// go-gopher
// go-gopher!
func
¶
- func TrimLeft(s [], cutset string) []
TrimLeft returns a subslice of s by slicing off all leading UTF-8-encoded code
points contained in cutset.
Example:
fmt.Print(string(bytes.TrimLeft([]byte(“453gopher8257”), “0123456789”)))
// Output:
// gopher8257
func
¶
TrimLeftFunc treats s as UTF-8-encoded bytes and returns a subslice of s by
slicing off all leading UTF-8-encoded code points c that satisfy f(c).
Example:
fmt.Println(string(bytes.TrimLeftFunc([]byte(“go-gopher”), unicode.IsLetter)))
fmt.Println(string(bytes.TrimLeftFunc([]byte(“go-gopher!”), unicode.IsPunct)))
fmt.Println(string(bytes.TrimLeftFunc([]byte(“1234go-gopher!567”), unicode.IsNumber)))
// Output:
// -gopher
// go-gopher!
// go-gopher!567
func TrimPrefix
- func TrimPrefix(s, prefix []byte) []
TrimPrefix returns s without the provided leading prefix string. If s doesn’t
start with prefix, s is returned unchanged.
Example:
var b = []byte(“Goodbye,, world!”)
b = bytes.TrimPrefix(b, []byte(“Goodbye,”))
b = bytes.TrimPrefix(b, []byte(“See ya,”))
fmt.Printf(“Hello%s”, b)
// Output: Hello, world!
func
¶
- func TrimRight(s [], cutset string) []
TrimRight returns a subslice of s by slicing off all trailing UTF-8-encoded code
points that are contained in cutset.
Example:
fmt.Print(string(bytes.TrimRight([]byte(“453gopher8257”), “0123456789”)))
// Output:
// 453gopher
func
¶
TrimRightFunc returns a subslice of s by slicing off all trailing UTF-8-encoded
code points c that satisfy f(c).
Example:
fmt.Println(string(bytes.TrimRightFunc([]byte(“go-gopher”), unicode.IsLetter)))
fmt.Println(string(bytes.TrimRightFunc([]byte(“go-gopher!”), unicode.IsPunct)))
fmt.Println(string(bytes.TrimRightFunc([]byte(“1234go-gopher!567”), unicode.IsNumber)))
// Output:
// go-
// go-gopher
// 1234go-gopher!
func TrimSpace
- func TrimSpace(s []byte) []
TrimSpace returns a subslice of s by slicing off all leading and trailing white
space, as defined by Unicode.
Example:
fmt.Printf(“%s”, bytes.TrimSpace([]byte(“ \t\n a lone gopher \n\t\r\n”)))
// Output: a lone gopher
func
¶
- func TrimSuffix(s, suffix []) []byte
TrimSuffix returns s without the provided trailing suffix string. If s doesn’t
end with suffix, s is returned unchanged.
Example:
var b = []byte(“Hello, goodbye, etc!”)
b = bytes.TrimSuffix(b, []byte(“goodbye, etc!”))
b = bytes.TrimSuffix(b, []byte(“gopher”))
b = append(b, bytes.TrimSuffix([]byte(“world!”), []byte(“x!”))…)
os.Stdout.Write(b)
// Output: Hello, world!
type Buffer
- type Buffer struct {
// contains filtered or unexported fields
}
A Buffer is a variable-sized buffer of bytes with Read and Write methods. The
zero value for Buffer is an empty buffer ready to use.
Example:
var b bytes.Buffer // A Buffer needs no initialization.
b.Write([]byte(“Hello “))
fmt.Fprintf(&b, “world!”)
b.WriteTo(os.Stdout)
// Output: Hello world!
Example:
// A Buffer can turn a string or a []byte into an io.Reader.
buf := bytes.NewBufferString(“R29waGVycyBydWxlIQ==”)
dec := base64.NewDecoder(base64.StdEncoding, buf)
io.Copy(os.Stdout, dec)
// Output: Gophers rule!
func NewBuffer
- func NewBuffer(buf []byte)
NewBuffer creates and initializes a new Buffer using buf as its initial
contents. The new Buffer takes ownership of buf, and the caller should not use
buf after this call. NewBuffer is intended to prepare a Buffer to read existing
data. It can also be used to size the internal buffer for writing. To do that,
buf should have the desired capacity but a length of zero.
In most cases, new(Buffer) (or just declaring a Buffer variable) is sufficient
to initialize a Buffer.
func NewBufferString
- func NewBufferString(s string)
NewBufferString creates and initializes a new Buffer using string s as its
initial contents. It is intended to prepare a buffer to read an existing string.
In most cases, new(Buffer) (or just declaring a Buffer variable) is sufficient
to initialize a Buffer.
func (Buffer) Bytes
- func (b Buffer) Bytes() []
Bytes returns a slice of length b.Len() holding the unread portion of the
buffer. The slice is valid for use only until the next buffer modification (that
is, only until the next call to a method like Read, Write, Reset, or Truncate).
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 (Buffer) Cap
- func (b Buffer) Cap()
Cap returns the capacity of the buffer’s underlying byte slice, that is, the
total space allocated for the buffer’s data.
func (Buffer) Grow
- func (b Buffer) Grow(n )
Grow grows the buffer’s capacity, if necessary, to guarantee space for another n
bytes. After Grow(n), at least n bytes can be written to the buffer without
another allocation. If n is negative, Grow will panic. If the buffer can’t grow
it will panic with ErrTooLarge.
Example:
var b bytes.Buffer
b.Grow(64)
bb := b.Bytes()
b.Write([]byte(“64 bytes or fewer”))
fmt.Printf(“%q”, bb[:b.Len()])
// Output: “64 bytes or fewer”
func (Buffer)
¶
- func (b ) Len() int
Len returns the number of bytes of the unread portion of the buffer; b.Len() ==
len(b.Bytes()).
func (Buffer)
¶
- func (b ) Next(n int) []
Next returns a slice containing the next n bytes from the buffer, advancing the
buffer as if the bytes had been returned by Read. If there are fewer than n
bytes in the buffer, Next returns the entire buffer. The slice is only valid
until the next call to a read or write method.
func (Buffer) Read
Read reads the next len(p) bytes from the buffer or until the buffer is drained.
The return value n is the number of bytes read. If the buffer has no data to
return, err is io.EOF (unless len(p) is zero); otherwise it is nil.
func (Buffer) ReadByte
ReadByte reads and returns the next byte from the buffer. If no byte is
available, it returns error io.EOF.
func (Buffer) ReadBytes
ReadBytes reads until the first occurrence of delim in the input, returning a
slice containing the data up to and including the delimiter. If ReadBytes
encounters an error before finding a delimiter, it returns the data read before
the error and the error itself (often io.EOF). ReadBytes returns err != nil if
and only if the returned data does not end in delim.
ReadFrom reads data from r until EOF and appends it to the buffer, growing the
buffer as needed. The return value n is the number of bytes read. Any error
except io.EOF encountered during the read is also returned. If the buffer
becomes too large, ReadFrom will panic with ErrTooLarge.
func (Buffer)
¶
ReadRune reads and returns the next UTF-8-encoded Unicode code point from the
buffer. If no bytes are available, the error returned is io.EOF. If the bytes
are an erroneous UTF-8 encoding, it consumes one byte and returns U+FFFD, 1.
func (Buffer)
¶
ReadString reads until the first occurrence of delim in the input, returning a
string containing the data up to and including the delimiter. If ReadString
encounters an error before finding a delimiter, it returns the data read before
the error and the error itself (often io.EOF). ReadString returns err != nil if
and only if the returned data does not end in delim.
func (Buffer)
¶
- func (b ) Reset()
Reset resets the buffer to be empty, but it retains the underlying storage for
use by future writes. Reset is the same as Truncate(0).
func (Buffer) String
- func (b Buffer) String()
String returns the contents of the unread portion of the buffer as a string. If
the Buffer is a nil pointer, it returns “
To build strings more efficiently, see the strings.Builder type.
func (Buffer) Truncate
- func (b Buffer) Truncate(n )
Truncate discards all but the first n unread bytes from the buffer but continues
to use the same allocated storage. It panics if n is negative or greater than
the length of the buffer.
func (Buffer) UnreadByte
- func (b Buffer) UnreadByte()
UnreadByte unreads the last byte returned by the most recent successful read
operation that read at least one byte. If a write has happened since the last
read, if the last read returned an error, or if the read read zero bytes,
UnreadByte returns an error.
func (Buffer) UnreadRune
- func (b Buffer) UnreadRune()
UnreadRune unreads the last rune returned by ReadRune. If the most recent read
or write operation on the buffer was not a successful ReadRune, UnreadRune
returns an error. (In this regard it is stricter than UnreadByte, which will
unread the last byte from any read operation.)
func (Buffer) Write
Write appends the contents of p to the buffer, growing the buffer as needed. The
return value n is the length of p; err is always nil. If the buffer becomes too
large, Write will panic with ErrTooLarge.
func (Buffer) WriteByte
WriteByte appends the byte c to the buffer, growing the buffer as needed. The
returned error is always nil, but is included to match bufio.Writer’s WriteByte.
If the buffer becomes too large, WriteByte will panic with ErrTooLarge.
func (Buffer)
¶
WriteRune appends the UTF-8 encoding of Unicode code point r to the buffer,
returning its length and an error, which is always nil but is included to match
bufio.Writer’s WriteRune. The buffer is grown as needed; if it becomes too
large, WriteRune will panic with ErrTooLarge.
func (Buffer)
¶
WriteString appends the contents of s to the buffer, growing the buffer as
needed. The return value n is the length of s; err is always nil. If the buffer
becomes too large, WriteString will panic with ErrTooLarge.
WriteTo writes data to w until the buffer is drained or an error occurs. The
return value n is the number of bytes written; it always fits into an int, but
it is int64 to match the io.WriterTo interface. Any error encountered during the
write is also returned.
- type Reader struct {
// contains filtered or unexported fields
}
A Reader implements the io.Reader, io.ReaderAt, io.WriterTo, io.Seeker,
io.ByteScanner, and io.RuneScanner interfaces by reading from a byte slice.
Unlike a Buffer, a Reader is read-only and supports seeking.
func NewReader
- func NewReader(b []byte)
NewReader returns a new Reader reading from b.
func (Reader) Len
- func (r Reader) Len()
Len returns the number of bytes of the unread portion of the slice.
Example:
fmt.Println(bytes.NewReader([]byte(“Hi!”)).Len())
fmt.Println(bytes.NewReader([]byte(“こんにちは!”)).Len())
// Output:
// 3
// 16
func (Reader)
¶
Read implements the io.Reader interface.
func (Reader)
¶
ReadAt implements the io.ReaderAt interface.
func (Reader) ReadByte
ReadByte implements the io.ByteReader interface.
func (Reader)
¶
ReadRune implements the io.RuneReader interface.
func (Reader)
¶
- func (r ) Reset(b []byte)
Reset resets the Reader to be reading from b.
func (Reader)
¶
Seek implements the io.Seeker interface.
func (Reader) Size
- func (r Reader) Size()
Size returns the original length of the underlying byte slice. Size is the
number of bytes available for reading via ReadAt. The returned value is always
the same and is not affected by calls to any other method.
func (Reader) UnreadByte
- func (r Reader) UnreadByte()
UnreadByte complements ReadByte in implementing the io.ByteScanner interface.
func (Reader) UnreadRune
- func (r Reader) UnreadRune()
UnreadRune complements ReadRune in implementing the io.RuneScanner interface.
WriteTo implements the io.WriterTo interface.
Bugs
- The rule Title uses for word boundaries does not handle Unicode punctuation
properly.