<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # package bytes `import "bytes"` bytes包實現了操作[]byte的常用函數。本包的函數和strings包的函數相當類似。 ## Index * [Constants](#pkg-constants) * [Variables](#pkg-variables) * [func Compare(a, b []byte) int](#Compare) * [func Equal(a, b []byte) bool](#Equal) * [func EqualFold(s, t []byte) bool](#EqualFold) * [func Runes(s []byte) []rune](#Runes) * [func HasPrefix(s, prefix []byte) bool](#HasPrefix) * [func HasSuffix(s, suffix []byte) bool](#HasSuffix) * [func Contains(b, subslice []byte) bool](#Contains) * [func Count(s, sep []byte) int](#Count) * [func Index(s, sep []byte) int](#Index) * [func IndexByte(s []byte, c byte) int](#IndexByte) * [func IndexRune(s []byte, r rune) int](#IndexRune) * [func IndexAny(s []byte, chars string) int](#IndexAny) * [func IndexFunc(s []byte, f func(r rune) bool) int](#IndexFunc) * [func LastIndex(s, sep []byte) int](#LastIndex) * [func LastIndexAny(s []byte, chars string) int](#LastIndexAny) * [func LastIndexFunc(s []byte, f func(r rune) bool) int](#LastIndexFunc) * [func Title(s []byte) []byte](#Title) * [func ToLower(s []byte) []byte](#ToLower) * [func ToLowerSpecial(\_case unicode.SpecialCase, s []byte) []byte](#ToLowerSpecial) * [func ToUpper(s []byte) []byte](#ToUpper) * [func ToUpperSpecial(\_case unicode.SpecialCase, s []byte) []byte](#ToUpperSpecial) * [func ToTitle(s []byte) []byte](#ToTitle) * [func ToTitleSpecial(\_case unicode.SpecialCase, s []byte) []byte](#ToTitleSpecial) * [func Repeat(b []byte, count int) []byte](#Repeat) * [func Replace(s, old, new []byte, n int) []byte](#Replace) * [func Map(mapping func(r rune) rune, s []byte) []byte](#Map) * [func Trim(s []byte, cutset string) []byte](#Trim) * [func TrimSpace(s []byte) []byte](#TrimSpace) * [func TrimFunc(s []byte, f func(r rune) bool) []byte](#TrimFunc) * [func TrimLeft(s []byte, cutset string) []byte](#TrimLeft) * [func TrimLeftFunc(s []byte, f func(r rune) bool) []byte](#TrimLeftFunc) * [func TrimPrefix(s, prefix []byte) []byte](#TrimPrefix) * [func TrimRight(s []byte, cutset string) []byte](#TrimRight) * [func TrimRightFunc(s []byte, f func(r rune) bool) []byte](#TrimRightFunc) * [func TrimSuffix(s, suffix []byte) []byte](#TrimSuffix) * [func Fields(s []byte) [][]byte](#Fields) * [func FieldsFunc(s []byte, f func(rune) bool) [][]byte](#FieldsFunc) * [func Split(s, sep []byte) [][]byte](#Split) * [func SplitN(s, sep []byte, n int) [][]byte](#SplitN) * [func SplitAfter(s, sep []byte) [][]byte](#SplitAfter) * [func SplitAfterN(s, sep []byte, n int) [][]byte](#SplitAfterN) * [func Join(s [][]byte, sep []byte) []byte](#Join) * [type Reader](#Reader) * [func NewReader(b []byte) \*Reader](#NewReader) * [func (r \*Reader) Len() int](#Reader.Len) * [func (r \*Reader) Read(b []byte) (n int, err error)](#Reader.Read) * [func (r \*Reader) ReadByte() (b byte, err error)](#Reader.ReadByte) * [func (r \*Reader) UnreadByte() error](#Reader.UnreadByte) * [func (r \*Reader) ReadRune() (ch rune, size int, err error)](#Reader.ReadRune) * [func (r \*Reader) UnreadRune() error](#Reader.UnreadRune) * [func (r \*Reader) Seek(offset int64, whence int) (int64, error)](#Reader.Seek) * [func (r \*Reader) ReadAt(b []byte, off int64) (n int, err error)](#Reader.ReadAt) * [func (r \*Reader) WriteTo(w io.Writer) (n int64, err error)](#Reader.WriteTo) * [type Buffer](#Buffer) * [func NewBuffer(buf []byte) \*Buffer](#NewBuffer) * [func NewBufferString(s string) \*Buffer](#NewBufferString) * [func (b \*Buffer) Reset()](#Buffer.Reset) * [func (b \*Buffer) Len() int](#Buffer.Len) * [func (b \*Buffer) Bytes() []byte](#Buffer.Bytes) * [func (b \*Buffer) String() string](#Buffer.String) * [func (b \*Buffer) Truncate(n int)](#Buffer.Truncate) * [func (b \*Buffer) Grow(n int)](#Buffer.Grow) * [func (b \*Buffer) Read(p []byte) (n int, err error)](#Buffer.Read) * [func (b \*Buffer) Next(n int) []byte](#Buffer.Next) * [func (b \*Buffer) ReadByte() (c byte, err error)](#Buffer.ReadByte) * [func (b \*Buffer) UnreadByte() error](#Buffer.UnreadByte) * [func (b \*Buffer) ReadRune() (r rune, size int, err error)](#Buffer.ReadRune) * [func (b \*Buffer) UnreadRune() error](#Buffer.UnreadRune) * [func (b \*Buffer) ReadBytes(delim byte) (line []byte, err error)](#Buffer.ReadBytes) * [func (b \*Buffer) ReadString(delim byte) (line string, err error)](#Buffer.ReadString) * [func (b \*Buffer) Write(p []byte) (n int, err error)](#Buffer.Write) * [func (b \*Buffer) WriteString(s string) (n int, err error)](#Buffer.WriteString) * [func (b \*Buffer) WriteByte(c byte) error](#Buffer.WriteByte) * [func (b \*Buffer) WriteRune(r rune) (n int, err error)](#Buffer.WriteRune) * [func (b \*Buffer) ReadFrom(r io.Reader) (n int64, err error)](#Buffer.ReadFrom) * [func (b \*Buffer) WriteTo(w io.Writer) (n int64, err error)](#Buffer.WriteTo) ### Examples * [Buffer](#example-Buffer) * [Buffer (Reader)](#example-Buffer--Reader) * [Compare](#example-Compare) * [Compare (Search)](#example-Compare--Search) * [TrimPrefix](#example-TrimPrefix) * [TrimSuffix](#example-TrimSuffix) ## Constants ``` const MinRead = 512 ``` MinRead是被Buffer.ReadFrom傳遞給Read調用的最小尺寸。只要該Buffer在保存內容之外有最少MinRead字節的余量,其ReadFrom方法就不會增加底層的緩沖。 ## Variables ``` var ErrTooLarge = errors.New("bytes.Buffer: too large") ``` 如果內存中不能申請足夠保存數據的緩沖,ErrTooLarge就會被傳遞給panic函數。 ## func [Compare](https://github.com/golang/go/blob/master/src/bytes/bytes_decl.go#L24 "View Source") ``` func Compare(a, b []byte) int ``` Compare函數返回一個整數表示兩個[]byte切片按字典序比較的結果(類同C的strcmp)。如果a==b返回0;如果a&lt;b返回-1;否則返回+1。nil參數視為空切片。 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 (Search) ``` // 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 [Equal](https://github.com/golang/go/blob/master/src/bytes/bytes_decl.go#L17 "View Source") ``` func Equal(a, b []byte) bool ``` 判斷兩個切片的內容是否完全相同。 ## func [EqualFold](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L646 "View Source") ``` func EqualFold(s, t []byte) bool ``` 判斷兩個utf-8編碼切片(將unicode大寫、小寫、標題三種格式字符視為相同)是否相同。 ## func [Runes](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L593 "View Source") ``` func Runes(s []byte) []rune ``` Runes函數返回和s等價的[]rune切片。(將utf-8編碼的unicode碼值分別寫入單個rune) ## func [HasPrefix](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L331 "View Source") ``` func HasPrefix(s, prefix []byte) bool ``` 判斷s是否有前綴切片prefix。 ## func [HasSuffix](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L336 "View Source") ``` func HasSuffix(s, suffix []byte) bool ``` 判斷s是否有后綴切片suffix。 ## func [Contains](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L81 "View Source") ``` func Contains(b, subslice []byte) bool ``` 判斷切片b是否包含子切片subslice。 ## func [Count](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L50 "View Source") ``` func Count(s, sep []byte) int ``` Count計算s中有多少個不重疊的sep子切片。 ## func [Index](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L86 "View Source") ``` func Index(s, sep []byte) int ``` 子切片sep在s中第一次出現的位置,不存在則返回-1。 ## func [IndexByte](https://github.com/golang/go/blob/master/src/bytes/bytes_decl.go#L10 "View Source") ``` func IndexByte(s []byte, c byte) int ``` 字符c在s中第一次出現的位置,不存在則返回-1。 ## func [IndexRune](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L143 "View Source") ``` func IndexRune(s []byte, r rune) int ``` unicode字符r的utf-8編碼在s中第一次出現的位置,不存在則返回-1。 ## func [IndexAny](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L158 "View Source") ``` func IndexAny(s []byte, chars string) int ``` 字符串chars中的任一utf-8編碼在s中第一次出現的位置,如不存在或者chars為空字符串則返回-1 ## func [IndexFunc](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L510 "View Source") ``` func IndexFunc(s []byte, f func(r rune) bool) int ``` s中第一個滿足函數f的位置i(該處的utf-8碼值r滿足f(r)==true),不存在則返回-1 ## func [LastIndex](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L126 "View Source") ``` func LastIndex(s, sep []byte) int ``` 切片sep在字符串s中最后一次出現的位置,不存在則返回-1。 ## func [LastIndexAny](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L183 "View Source") ``` func LastIndexAny(s []byte, chars string) int ``` 字符串chars中的任一utf-8字符在s中最后一次出現的位置,如不存在或者chars為空字符串則返回-1。 ## func [LastIndexFunc](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L517 "View Source") ``` func LastIndexFunc(s []byte, f func(r rune) bool) int ``` s中最后一個滿足函數f的unicode碼值的位置i,不存在則返回-1。 ## func [Title](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L443 "View Source") ``` func Title(s []byte) []byte ``` 返回s中每個單詞的首字母都改為標題格式的拷貝。 BUG: Title用于劃分單詞的規則不能很好的處理Unicode標點符號。 ## func [ToLower](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L391 "View Source") ``` func ToLower(s []byte) []byte ``` 返回將所有字母都轉為對應的小寫版本的拷貝。 ## func [ToLowerSpecial](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L404 "View Source") ``` func ToLowerSpecial(_case unicode.SpecialCase, s []byte) []byte ``` 使用\_case規定的字符映射,返回將所有字母都轉為對應的小寫版本的拷貝。 ## func [ToUpper](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L388 "View Source") ``` func ToUpper(s []byte) []byte ``` 返回將所有字母都轉為對應的大寫版本的拷貝。 ## func [ToUpperSpecial](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L398 "View Source") ``` func ToUpperSpecial(_case unicode.SpecialCase, s []byte) []byte ``` 使用\_case規定的字符映射,返回將所有字母都轉為對應的大寫版本的拷貝。 ## func [ToTitle](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L394 "View Source") ``` func ToTitle(s []byte) []byte ``` 返回將所有字母都轉為對應的標題版本的拷貝。 ## func [ToTitleSpecial](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L410 "View Source") ``` func ToTitleSpecial(_case unicode.SpecialCase, s []byte) []byte ``` 使用\_case規定的字符映射,返回將所有字母都轉為對應的標題版本的拷貝。 ## func [Repeat](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L378 "View Source") ``` func Repeat(b []byte, count int) []byte ``` 返回count個b串聯形成的新的切片。 ## func [Replace](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L608 "View Source") ``` func Replace(s, old, new []byte, n int) []byte ``` 返回將s中前n個不重疊old切片序列都替換為new的新的切片拷貝,如果n&lt;0會替換所有old子切片。 ## func [Map](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L344 "View Source") ``` func Map(mapping func(r rune) rune, s []byte) []byte ``` 將s的每一個unicode碼值r都替換為mapping(r),返回這些新碼值組成的切片拷貝。如果mapping返回一個負值,將會丟棄該碼值而不會被替換(返回值中對應位置將沒有碼值)。 ## func [Trim](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L570 "View Source") ``` func Trim(s []byte, cutset string) []byte ``` 返回將s前后端所有cutset包含的unicode碼值都去掉的子切片。(共用底層數組) ## func [TrimSpace](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L588 "View Source") ``` func TrimSpace(s []byte) []byte ``` 返回將s前后端所有空白(unicode.IsSpace指定)都去掉的子切片。(共用底層數組) ## func [TrimFunc](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L485 "View Source") ``` func TrimFunc(s []byte, f func(r rune) bool) []byte ``` 返回將s前后端所有滿足f的unicode碼值都去掉的子切片。(共用底層數組) ## func [TrimLeft](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L576 "View Source") ``` func TrimLeft(s []byte, cutset string) []byte ``` 返回將s前端所有cutset包含的unicode碼值都去掉的子切片。(共用底層數組) ## func [TrimLeftFunc](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L462 "View Source") ``` func TrimLeftFunc(s []byte, f func(r rune) bool) []byte ``` 返回將s前端所有滿足f的unicode碼值都去掉的子切片。(共用底層數組) ## func [TrimPrefix](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L491 "View Source") ``` func TrimPrefix(s, prefix []byte) []byte ``` 返回去除s可能的前綴prefix的子切片。(共用底層數組) 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 [TrimRight](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L582 "View Source") ``` func TrimRight(s []byte, cutset string) []byte ``` 返回將s后端所有cutset包含的unicode碼值都去掉的子切片。(共用底層數組) ## func [TrimRightFunc](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L472 "View Source") ``` func TrimRightFunc(s []byte, f func(r rune) bool) []byte ``` 返回將s后端所有滿足f的unicode碼值都去掉的子切片。(共用底層數組) ## func [TrimSuffix](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L500 "View Source") ``` func TrimSuffix(s, suffix []byte) []byte ``` 返回去除s可能的后綴suffix的子切片。(共用底層數組) 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! ``` ## func [Fields](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L262 "View Source") ``` func Fields(s []byte) [][]byte ``` 返回將字符串按照空白(unicode.IsSpace確定,可以是一到多個連續的空白字符)分割的多個子切片。如果字符串全部是空白或者是空字符串的話,會返回空切片。 ## func [FieldsFunc](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L270 "View Source") ``` func FieldsFunc(s []byte, f func(rune) bool) [][]byte ``` 類似Fields,但使用函數f來確定分割符(滿足f的utf-8碼值)。如果字符串全部是分隔符或者是空字符串的話,會返回空切片。 ## func [Split](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L250 "View Source") ``` func Split(s, sep []byte) [][]byte ``` 用去掉s中出現的sep的方式進行分割,會分割到結尾,并返回生成的所有[]byte切片組成的切片(每一個sep都會進行一次切割,即使兩個sep相鄰,也會進行兩次切割)。如果sep為空字符,Split會將s切分成每一個unicode碼值一個[]byte切片。 ## func [SplitN](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L233 "View Source") ``` func SplitN(s, sep []byte, n int) [][]byte ``` 用去掉s中出現的sep的方式進行分割,會分割到最多n個子切片,并返回生成的所有[]byte切片組成的切片(每一個sep都會進行一次切割,即使兩個sep相鄰,也會進行兩次切割)。如果sep為空字符,Split會將s切分成每一個unicode碼值一個[]byte切片。參數n決定返回的切片的數目: ``` n > 0 : 返回的切片最多n個子字符串;最后一個子字符串包含未進行切割的部分。 n == 0: 返回nil n < 0 : 返回所有的子字符串組成的切片 ``` ## func [SplitAfter](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L256 "View Source") ``` func SplitAfter(s, sep []byte) [][]byte ``` 用從s中出現的sep后面切斷的方式進行分割,會分割到結尾,并返回生成的所有[]byte切片組成的切片(每一個sep都會進行一次切割,即使兩個sep相鄰,也會進行兩次切割)。如果sep為空字符,Split會將s切分成每一個unicode碼值一個[]byte切片。 ## func [SplitAfterN](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L242 "View Source") ``` func SplitAfterN(s, sep []byte, n int) [][]byte ``` 用從s中出現的sep后面切斷的方式進行分割,會分割到最多n個子切片,并返回生成的所有[]byte切片組成的切片(每一個sep都會進行一次切割,即使兩個sep相鄰,也會進行兩次切割)。如果sep為空字符,Split會將s切分成每一個unicode碼值一個[]byte切片。參數n決定返回的切片的數目: ``` n > 0 : 返回的切片最多n個子字符串;最后一個子字符串包含未進行切割的部分。 n == 0: 返回nil n < 0 : 返回所有的子字符串組成的切片 ``` ## func [Join](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L308 "View Source") ``` func Join(s [][]byte, sep []byte) []byte ``` 將一系列[]byte切片連接為一個[]byte切片,之間用sep來分隔,返回生成的新切片。 ## type [Reader](https://github.com/golang/go/blob/master/src/bytes/reader.go#L17 "View Source") ``` type Reader struct { // 內含隱藏或非導出字段 } ``` Reader類型通過從一個[]byte讀取數據,實現了io.Reader、io.Seeker、io.ReaderAt、io.WriterTo、io.ByteScanner、io.RuneScanner接口。 ### func [NewReader](https://github.com/golang/go/blob/master/src/bytes/reader.go#L144 "View Source") ``` func NewReader(b []byte) *Reader ``` NewReader創建一個從s讀取數據的Reader。 ### func (\*Reader) [Len](https://github.com/golang/go/blob/master/src/bytes/reader.go#L25 "View Source") ``` func (r *Reader) Len() int ``` Len返回r包含的切片中還沒有被讀取的部分。 ### func (\*Reader) [Read](https://github.com/golang/go/blob/master/src/bytes/reader.go#L32 "View Source") ``` func (r *Reader) Read(b []byte) (n int, err error) ``` ### func (\*Reader) [ReadByte](https://github.com/golang/go/blob/master/src/bytes/reader.go#L60 "View Source") ``` func (r *Reader) ReadByte() (b byte, err error) ``` ### func (\*Reader) [UnreadByte](https://github.com/golang/go/blob/master/src/bytes/reader.go#L70 "View Source") ``` func (r *Reader) UnreadByte() error ``` ### func (\*Reader) [ReadRune](https://github.com/golang/go/blob/master/src/bytes/reader.go#L79 "View Source") ``` func (r *Reader) ReadRune() (ch rune, size int, err error) ``` ### func (\*Reader) [UnreadRune](https://github.com/golang/go/blob/master/src/bytes/reader.go#L94 "View Source") ``` func (r *Reader) UnreadRune() error ``` ### func (\*Reader) [Seek](https://github.com/golang/go/blob/master/src/bytes/reader.go#L104 "View Source") ``` func (r *Reader) Seek(offset int64, whence int) (int64, error) ``` Seek實現了io.Seeker接口。 ### func (\*Reader) [ReadAt](https://github.com/golang/go/blob/master/src/bytes/reader.go#L45 "View Source") ``` func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) ``` ### func (\*Reader) [WriteTo](https://github.com/golang/go/blob/master/src/bytes/reader.go#L125 "View Source") ``` func (r *Reader) WriteTo(w io.Writer) (n int64, err error) ``` WriteTo實現了io.WriterTo接口。 ## type [Buffer](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L17 "View Source") ``` type Buffer struct { // 內含隱藏或非導出字段 } ``` Buffer是一個實現了讀寫方法的可變大小的字節緩沖。本類型的零值是一個空的可用于讀寫的緩沖。 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 (Reader) ``` // 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](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L402 "View Source") ``` func NewBuffer(buf []byte) *Buffer ``` NewBuffer使用buf作為初始內容創建并初始化一個Buffer。本函數用于創建一個用于讀取已存在數據的buffer;也用于指定用于寫入的內部緩沖的大小,此時,buf應為一個具有指定容量但長度為0的切片。buf會被作為返回值的底層緩沖切片。 大多數情況下,new(Buffer)(或只是聲明一個Buffer類型變量)就足以初始化一個Buffer了。 ### func [NewBufferString](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L410 "View Source") ``` func NewBufferString(s string) *Buffer ``` NewBuffer使用s作為初始內容創建并初始化一個Buffer。本函數用于創建一個用于讀取已存在數據的buffer。 大多數情況下,new(Buffer)(或只是聲明一個Buffer類型變量)就足以初始化一個Buffer了。 ### func (\*Buffer) [Reset](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L75 "View Source") ``` func (b *Buffer) Reset() ``` Reset重設緩沖,因此會丟棄全部內容,等價于b.Truncate(0)。 ### func (\*Buffer) [Len](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L57 "View Source") ``` func (b *Buffer) Len() int ``` 返回緩沖中未讀取部分的字節長度;b.Len() == len(b.Bytes())。 ### func (\*Buffer) [Bytes](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L43 "View Source") ``` func (b *Buffer) Bytes() []byte ``` 返回未讀取部分字節數據的切片,len(b.Bytes()) == b.Len()。如果中間沒有調用其他方法,修改返回的切片的內容會直接改變Buffer的內容。 ### func (\*Buffer) [String](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L47 "View Source") ``` func (b *Buffer) String() string ``` 將未讀取部分的字節數據作為字符串返回,如果b是nil指針,會返回"&lt;nil&gt;"。 ### func (\*Buffer) [Truncate](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L61 "View Source") ``` func (b *Buffer) Truncate(n int) ``` 丟棄緩沖中除前n字節數據外的其它數據,如果n小于零或者大于緩沖容量將panic。 ### func (\*Buffer) [Grow](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L114 "View Source") ``` func (b *Buffer) Grow(n int) ``` 必要時會增加緩沖的容量,以保證n字節的剩余空間。調用Grow(n)后至少可以向緩沖中寫入n字節數據而無需申請內存。如果n小于零或者不能增加容量都會panic。 ### func (\*Buffer) [Read](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L251 "View Source") ``` func (b *Buffer) Read(p []byte) (n int, err error) ``` Read方法從緩沖中讀取數據直到緩沖中沒有數據或者讀取了len(p)字節數據,將讀取的數據寫入p。返回值n是讀取的字節數,除非緩沖中完全沒有數據可以讀取并寫入p,此時返回值err為io.EOF;否則err總是nil。 ### func (\*Buffer) [Next](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L273 "View Source") ``` func (b *Buffer) Next(n int) []byte ``` 返回未讀取部分前n字節數據的切片,并且移動讀取位置,就像調用了Read方法一樣。如果緩沖內數據不足,會返回整個數據的切片。切片只在下一次調用b的讀/寫方法前才合法。 ### func (\*Buffer) [ReadByte](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L289 "View Source") ``` func (b *Buffer) ReadByte() (c byte, err error) ``` ReadByte讀取并返回緩沖中的下一個字節。如果沒有數據可用,返回值err為io.EOF。 ### func (\*Buffer) [UnreadByte](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L345 "View Source") ``` func (b *Buffer) UnreadByte() error ``` UnreadByte吐出最近一次讀取操作讀取的最后一個字節。如果最后一次讀取操作之后進行了寫入,本方法會返回錯誤。 ### func (\*Buffer) [ReadRune](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L307 "View Source") ``` func (b *Buffer) ReadRune() (r rune, size int, err error) ``` ReadRune讀取并返回緩沖中的下一個utf-8碼值。如果沒有數據可用,返回值err為io.EOF。如果緩沖中的數據是錯誤的utf-8編碼,本方法會吃掉一字節并返回(U+FFFD, 1, nil)。 ### func (\*Buffer) [UnreadRune](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L330 "View Source") ``` func (b *Buffer) UnreadRune() error ``` UnreadRune吐出最近一次調用ReadRune方法讀取的unicode碼值。如果最近一次讀寫操作不是ReadRune,本方法會返回錯誤。(這里就能看出來UnreadRune比UnreadByte嚴格多了) ### func (\*Buffer) [ReadBytes](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L362 "View Source") ``` func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) ``` ReadBytes讀取直到第一次遇到delim字節,返回一個包含已讀取的數據和delim字節的切片。如果ReadBytes方法在讀取到delim之前遇到了錯誤,它會返回在錯誤之前讀取的數據以及該錯誤(一般是io.EOF)。當且僅當ReadBytes方法返回的切片不以delim結尾時,會返回一個非nil的錯誤。 ### func (\*Buffer) [ReadString](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L390 "View Source") ``` func (b *Buffer) ReadString(delim byte) (line string, err error) ``` ReadString讀取直到第一次遇到delim字節,返回一個包含已讀取的數據和delim字節的字符串。如果ReadString方法在讀取到delim之前遇到了錯誤,它會返回在錯誤之前讀取的數據以及該錯誤(一般是io.EOF)。當且僅當ReadString方法返回的切片不以delim結尾時,會返回一個非nil的錯誤。 ### func (\*Buffer) [Write](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L125 "View Source") ``` func (b *Buffer) Write(p []byte) (n int, err error) ``` Write將p的內容寫入緩沖中,如必要會增加緩沖容量。返回值n為len(p),err總是nil。如果緩沖變得太大,Write會采用錯誤值ErrTooLarge引發panic。 ### func (\*Buffer) [WriteString](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L134 "View Source") ``` func (b *Buffer) WriteString(s string) (n int, err error) ``` Write將s的內容寫入緩沖中,如必要會增加緩沖容量。返回值n為len(p),err總是nil。如果緩沖變得太大,Write會采用錯誤值ErrTooLarge引發panic。 ### func (\*Buffer) [WriteByte](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L226 "View Source") ``` func (b *Buffer) WriteByte(c byte) error ``` WriteByte將字節c寫入緩沖中,如必要會增加緩沖容量。返回值總是nil,但仍保留以匹配bufio.Writer的WriteByte方法。如果緩沖太大,WriteByte會采用錯誤值ErrTooLarge引發panic。 ### func (\*Buffer) [WriteRune](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L237 "View Source") ``` func (b *Buffer) WriteRune(r rune) (n int, err error) ``` WriteByte將unicode碼值r的utf-8編碼寫入緩沖中,如必要會增加緩沖容量。返回值總是nil,但仍保留以匹配bufio.Writer的WriteRune方法。如果緩沖太大,WriteRune會采用錯誤值ErrTooLarge引發panic。 ### func (\*Buffer) [ReadFrom](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L150 "View Source") ``` func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) ``` ReadFrom從r中讀取數據直到結束并將讀取的數據寫入緩沖中,如必要會增加緩沖容量。返回值n為從r讀取并寫入b的字節數;會返回讀取時遇到的除了io.EOF之外的錯誤。如果緩沖太大,ReadFrom會采用錯誤值ErrTooLarge引發panic。 ### func (\*Buffer) [WriteTo](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L198 "View Source") ``` func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) ``` WriteTo從緩沖中讀取數據直到緩沖內沒有數據或遇到錯誤,并將這些數據寫入w。返回值n為從b讀取并寫入w的字節數;返回值總是可以無溢出的寫入int類型,但為了匹配io.WriterTo接口設為int64類型。從b讀取是遇到的非io.EOF錯誤及寫入w時遇到的錯誤都會終止本方法并返回該錯誤。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看