golang中bytes包用法

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

本文轉自 Golove的部落格http://www.cnblogs.com/golove/p/3287729.html

bytes 包中的函數和方法



// bytes 包實現了用於操作 []byte 的函數,類似於 strings 包中的函數

// bytes.go

------------------------------------------------------------

// Compare 用於比較兩個 []byte,並返回 int 型結果
// a == b 返回 0
// a < b 返回 -1
// a > b 返回 1
// 如果參數為 nil 則相當於傳入一個空 []byte
func Compare(a, b []byte) int

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// Count 計算 sep 在 s 中的非重疊個數
// 如果 sep 為空白,則返回 s 中的字元個數 + 1
func Count(s, sep []byte) int

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// Contains 判斷 b 中是否包含 subslice
// 如果 subslice 為空白,則返回 true
func Contains(b, subslice []byte) bool

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// Index 返回 sep 在 s 中第一次出現的位置
// 如果找不到,則返回 -1,如果 sep 為空白,則返回 0
func Index(s, sep []byte) int

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// LastIndex 返回 sep 在 s 中最後一次出現的位元組位置
// 如果找不到,則返回 -1,如果 sep 為空白,則返回 s 的長度
func LastIndex(s, sep []byte) int

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// IndexRune 返回 r 的編碼在 s 中第一次出現的位置
// 如果找不到,則返回 -1
func IndexRune(s []byte, r rune) int

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// IndexAny 返回 chars 中的任何一個字元在 s 中第一次出現的位置
// 如果找不到,則返回 -1,如果 chars 為空白,則返回 -1
func IndexAny(s []byte, chars string) int

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// LastIndexAny 返回 chars 中的任何一個字元在 s 中最後一次出現的位置
// 如果找不到,則返回 -1,如果 chars 為空白,也返回 -1
func LastIndexAny(s []byte, chars string) int

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// SplitN 以 sep 為分隔字元,將 s 切分成多個子串,結果中不包含 sep 本身
// 如果 sep 為空白,則將 s 切分成 Unicode 字元列表
// 如果 s 中沒有 sep,則將整個 s 作為 [][]byte 的第一個元素返回
// 參數 n 表示最多切分出幾個子串,超出的部分將不再切分
// 如果 n 為 0,則返回 nil,如果 n 小於 0,則不限制切分個數,全部切分
func SplitN(s, sep []byte, n int) [][]byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// SplitAfterN 和 SplitN 的功能一樣,只不過結果中包含 sep
func SplitAfterN(s, sep []byte, n int) [][]byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// Split 以 sep 為分隔字元,將 s 切分成多個子串,結果中不包含 sep 本身
// 如果 sep 為空白,則將 s 切分成 Unicode 字元列表。
// 如果 s 中沒有 sep 子串,則將整個 s 作為 []string 的第一個元素返回
func Split(s, sep []byte) [][]byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// SplitAfter 和 Split 的功能一樣,只不過結果中包含 sep
func SplitAfter(s, sep []byte) [][]byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// Fields 以連續的空白字元為分隔字元,將 s 切分成多個子串,結果中不包含空白字元本身
// 空白字元有:\t, \n, \v, \f, \r, ' ', U+0085 (NEL), U+00A0 (NBSP)
// 如果 s 中只包含空白字元,則返回一個空列表
func Fields(s []byte) [][]byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// FieldsFunc 以一個或多個連續的滿足 f(rune) 的字元為分隔字元,
// 將 s 切分成多個子串,結果中不包含分隔字元本身
// 如果 s 中沒有滿足 f(rune) 的字元,則返回一個空列表
func FieldsFunc(s []byte, f func(rune) bool) [][]byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// Join 將 s 中的子串聯接成一個單獨的 []byte,子串之間用 sep 分隔
func Join(s [][]byte, sep []byte) []byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// HasPrefix 判斷 s 是否以 prefix 開頭
// 如果 prefix 為空白,也返回 true
func HasPrefix(s, prefix []byte) bool

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// HasPrefix 判斷 s 是否以 prefix 結尾
// 如果 suffix 為空白,也返回 true
func HasSuffix(s, suffix []byte) bool

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// Map 將 s 中滿足 mapping(rune) 的字元替換為 mapping(rune) 的傳回值
// 如果 mapping(rune) 返回負數,則相應的字元將被刪除
func Map(mapping func(r rune) rune, s []byte) []byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// Repeat 將 count 個 b 串連成一個新的 []byte
func Repeat(b []byte, count int) []byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// ToUpper 將 s 中的所有字元修改為其大寫格式
// 對於非 ASCII 字元,它的大寫格式需要查錶轉換
func ToUpper(s []byte) []byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// ToLower 將 s 中的所有字元修改為其小寫格式
// 對於非 ASCII 字元,它的小寫格式需要查錶轉換
func ToLower(s []byte) []byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// ToTitle 將 s 中的所有字元修改為其 Title 格式
// 大部分字元的 Title 格式就是其 Upper 格式
// 只有少數字元的 Title 格式是特殊字元
// 這裡的 ToTitle 主要給 Title 函數調用
func ToTitle(s []byte) []byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// ToUpperSpecial 將 s 中的所有字元修改為其大寫格式
// 優先使用 _case 中的規則進行轉換
func ToUpperSpecial(_case unicode.SpecialCase, s []byte) []byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// ToLowerSpecial 將 s 中的所有字元修改為其小寫格式
// 優先使用 _case 中的規則進行轉換
func ToLowerSpecial(_case unicode.SpecialCase, s []byte) []byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// ToTitleSpecial 將 s 中的所有字元修改為其 Title 格式
// 優先使用 _case 中的規則進行轉換
func ToTitleSpecial(_case unicode.SpecialCase, s []byte) []byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// Title 將 s 中的所有單詞的首字母修改為其 Title 格式
// BUG: Title 規則不能正確處理 Unicode 標點符號
func Title(s []byte) []byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// TrimLeftFunc 將刪除 s 頭部連續的滿足 f(rune) 的字元
func TrimLeftFunc(s []byte, f func(r rune) bool) []byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// TrimRightFunc 將刪除 s 尾部連續的滿足 f(rune) 的字元
func TrimRightFunc(s []byte, f func(r rune) bool) []byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// TrimFunc 將刪除 s 首尾連續的滿足 f(rune) 的字元
func TrimFunc(s []byte, f func(r rune) bool) []byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// TrimPrefix 刪除 s 頭部的 prefix 字串
// 如果 s 不是以 prefix 開頭,則返回原始 s
func TrimPrefix(s, prefix []byte) []byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// TrimSuffix 刪除 s 尾部的 suffix 字串
// 如果 s 不是以 suffix 結尾,則返回原始 s
func TrimSuffix(s, suffix []byte) []byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// IndexFunc 返回 s 中第一個滿足 f(rune) 的字元的位元組位置
// 如果沒有滿足 f(rune) 的字元,則返回 -1
func IndexFunc(s []byte, f func(r rune) bool) int

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// LastIndexFunc 返回 s 中最後一個滿足 f(rune) 的字元的位元組位置
// 如果沒有滿足 f(rune) 的字元,則返回 -1
func LastIndexFunc(s []byte, f func(r rune) bool) int

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// Trim 將刪除 s 首尾連續的包含在 cutset 中的字元
func Trim(s []byte, cutset string) []byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// TrimLeft 將刪除 s 頭部連續的包含在 cutset 中的字元
func TrimLeft(s []byte, cutset string) []byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// TrimRight 將刪除 s 尾部連續的包含在 cutset 中的字元
func TrimRight(s []byte, cutset string) []byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// TrimSpace 將刪除 s 首尾連續的的空白字元
func TrimSpace(s []byte) []byte

------------------------------------------------------------

// Runes 將 s 切分為 Unicode 碼點列表
func Runes(s []byte) []rune

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// Replace 將 s 中的 old 子串替換為 new 子串並返回
// 替換次數為 n 次,如果 n 為 -1,則全部替換
// 如果 old 為空白,則在每個字元之間都插入一個 new
func Replace(s, old, new []byte, n int) []byte

------------------------------------------------------------

// 功能類似於 strings 包中的同名函數
// EqualFold 判斷 s 和 t 是否相等。忽略大小寫,同時它還會對特殊字元進行轉換
// 比如將“ϕ”轉換為“Φ”、將“DŽ”轉換為“Dž”等,然後再進行比較
func EqualFold(s, t []byte) bool



============================================================



// bytes_decl.go

------------------------------------------------------------

// IndexByte 返回 c 在 s 中第一次出現的位置
// 如果找不到,則返回 -1
func IndexByte(s []byte, c byte) int

------------------------------------------------------------

// Equal 判斷 a 和 b 是否相等
// 如果參數為 nil,則被視為空白 []byte
func Equal(a, b []byte) bool



============================================================



// reader.go

------------------------------------------------------------

type Reader struct {
// 私人欄位
}

// 通過 []byte 建立一個 bytes.Reader 對象
func NewReader(b []byte) *Reader

------------------------------------------------------------

// 返回 r 中未讀取的資料的長度
func (r *Reader) Len() int

------------------------------------------------------------

// 實現 io.Reader 介面
func (r *Reader) Read(b []byte) (n int, err error)

------------------------------------------------------------

// 實現 io.ReaderAt 介面
func (r *Reader) ReadAt(b []byte, off int64) (n int, err error)

------------------------------------------------------------

// 實現 io.ByteScanner 介面
func (r *Reader) ReadByte() (b byte, err error)
func (r *Reader) UnreadByte() error

------------------------------------------------------------

// 實現 io.RuneScanner 介面
func (r *Reader) ReadRune() (ch rune, size int, err error)
func (r *Reader) UnreadRune() error

------------------------------------------------------------

// 實現 io.Seeker 介面
func (r *Reader) Seek(offset int64, whence int) (int64, error)

------------------------------------------------------------

// 實現 io.WriterTo 介面
func (r *Reader) WriteTo(w io.Writer) (n int64, err error)



============================================================



// buffer.go

------------------------------------------------------------

// Buffer 實現了帶緩衝的輸入輸出操作
// 緩衝的容量會根據需要自動擴充
// 如果緩衝太大,無法繼續擴充,則會引發 panic(ErrTooLarge)
type Buffer struct {
// 私人欄位
}

// 通過 []byte 或 string 建立 bytes.Buffer 對象
func NewBuffer(buf []byte) *Buffer
func NewBufferString(s string) *Buffer

------------------------------------------------------------

// 返回 b 中資料的切片
func (b *Buffer) Bytes() []byte

------------------------------------------------------------

// 返回 b 中取資料的副本
func (b *Buffer) String() string

------------------------------------------------------------

// 返回 b 中資料的長度
func (b *Buffer) Len() int

------------------------------------------------------------

// Truncate 將緩衝區中的資料截短為前 n 位元組,截掉的部分將被丟棄
// 如果 n 是負數或 n 超出了緩衝區總長度,則會引發 panic
func (b *Buffer) Truncate(n int)

------------------------------------------------------------

// Reset 方法用於重設緩衝,即清空緩衝中的資料
// b.Reset() 相當於 b.Truncate(0)。
func (b *Buffer) Reset()

------------------------------------------------------------

// Grow 將緩衝長度向後擴充 n 個位元組,不返回任何資料。
// 如果 n 是複數,將引發 panic,如果無法擴充緩衝長度,則會引發 panic(ErrTooLarge)
func (b *Buffer) Grow(n int)

------------------------------------------------------------

// 實現了 io.Writer 介面
func (b *Buffer) Write(p []byte) (n int, err error)

------------------------------------------------------------

// 功能同 Write,只不過參數為字串類型
func (b *Buffer) WriteString(s string) (n int, err error)

------------------------------------------------------------

// 實現 io.ReaderFrom 介面
func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)

------------------------------------------------------------

// 實現了 io.WriterTo 介面
func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)

------------------------------------------------------------

// 實現了 io.ByteWriter 介面
func (b *Buffer) WriteByte(c byte) error

------------------------------------------------------------

// WriteRune 將一個字元 r 寫入到對象的資料流中
// 返回寫入過程中遇到的任何錯誤
func (b *Buffer) WriteRune(r rune) (n int, err error)

------------------------------------------------------------

// 實現了 io.Reader 介面
func (b *Buffer) Read(p []byte) (n int, err error)

------------------------------------------------------------

// Next 讀出緩衝中前 n 個位元組的資料,並返回其引用
// 如果 n 大於緩衝中資料的長度,則讀出所有資料
// 被讀出的資料在下一次讀寫操作之前是有效
// 下一次讀寫操作時,所引用的資料可能會被覆蓋
func (b *Buffer) Next(n int) []byte

------------------------------------------------------------

// 實現 io.ByteScanner 介面
func (b *Buffer) ReadByte() (c byte, err error)
func (b *Buffer) UnreadByte() error

------------------------------------------------------------

// 實現 io.RuneScanner 介面
func (b *Buffer) ReadRune() (r rune, size int, err error)
func (b *Buffer) UnreadRune() error

------------------------------------------------------------

// 類似於 bufio 包中的 Reader.ReadBytes 和 Reader.ReadString 方法
// ReadBytes 在 b 中尋找 delim 並讀出 delim 及其之前的所有資料
// ReadString 功能同 ReadBytes,只不過返回的是一個字串
func (b *Buffer) ReadBytes(delim byte) (line []byte, err error)
func (b *Buffer) ReadString(delim byte) (line string, err error)





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.