GoLang之strings、buffers、bytes、binary包


 

strings包

strings包的使用舉例:

package main
import s "strings"
import "fmt"

var p = fmt.Println

func main() {
    p("Contains:  ", s.Contains("test", "es"))
    p("Count:     ", s.Count("test", "t"))
    p("HasPrefix: ", s.HasPrefix("test", "te"))
    p("HasSuffix: ", s.HasSuffix("test", "st"))
    p("Index:     ", s.Index("test", "e"))
    p("Join:      ", s.Join([]string{"a", "b"}, "-"))
    p("Repeat:    ", s.Repeat("a", 5))
    p("Replace:   ", s.Replace("foo", "o", "0", -1))
    p("Replace:   ", s.Replace("foo", "o", "0", 1))
    p("Split:     ", s.Split("a-b-c-d-e", "-"))
    p("ToLower:   ", s.ToLower("TEST"))
    p("ToUpper:   ", s.ToUpper("test"))
    p()
    p("Len: ", len("hello"))
    p("Char:", "hello"[1])
}

 

 

 

 

bytes包

 

1、大小寫轉換

func ToUpper(s []byte) []byte { return Map(unicode.ToUpper, s) }
func ToLower(s []byte) []byte { return Map(unicode.ToLower, s) }
func ToTitle(s []byte) []byte { return Map(unicode.ToTitle, s) }

 

2、比較

func Compare(a, b []byte) int
func Equal(a, b []byte) bool
func EqualFold(s, t []byte) bool

 

3、替換

// 將 s 中前 n 個 old 替換為 new,n < 0 則替換全部。
func Replace(s, old, new []byte, n int) []byte

// 將 s 中的字符替換為 mapping(r) 的返回值,
// 如果 mapping 返回負值,則丟棄該字符。
func Map(mapping func(r rune) rune, s []byte) []byte

// 將 s 轉換為 []rune 類型返回
func Runes(s []byte) []rune

 

 

4、清除

// 去掉 s 兩邊(左邊、右邊)包含在 cutset 中的字符(返回 s 的切片)
func Trim(s []byte, cutset string) []byte
func TrimLeft(s []byte, cutset string) []byte
func TrimRight(s []byte, cutset string) []byte

// 去掉 s 兩邊(左邊、右邊)符合 f 要求的字符(返回 s 的切片)
func TrimFunc(s []byte, f func(r rune) bool) []byte
func TrimLeftFunc(s []byte, f func(r rune) bool) []byte
func TrimRightFunc(s []byte, f func(r rune) bool) []byte

// 去掉 s 兩邊的空白(unicode.IsSpace)(返回 s 的切片)
func TrimSpace(s []byte) []byte

// 去掉 s 的前綴 prefix(后綴 suffix)(返回 s 的切片)
func TrimPrefix(s, prefix []byte) []byte
func TrimSuffix(s, suffix []byte) []byte

 

5、分割、連接

// Split 以 sep 為分隔符將 s 切分成多個子串,結果不包含分隔符。
// 如果 sep 為空,則將 s 切分成 Unicode 字符列表。
// SplitN 可以指定切分次數 n,超出 n 的部分將不進行切分。
func Split(s, sep []byte) [][]byte
func SplitN(s, sep []byte, n int) [][]byte

// 功能同 Split,只不過結果包含分隔符(在各個子串尾部)。
func SplitAfter(s, sep []byte) [][]byte
func SplitAfterN(s, sep []byte, n int) [][]byte

// 以連續空白為分隔符將 s 切分成多個子串,結果不包含分隔符。
func Fields(s []byte) [][]byte

// 以符合 f 的字符為分隔符將 s 切分成多個子串,結果不包含分隔符。
func FieldsFunc(s []byte, f func(rune) bool) [][]byte

// 以 sep 為連接符,將子串列表 s 連接成一個字節串。
func Join(s [][]byte, sep []byte) []byte

// 將子串 b 重復 count 次后返回。
func Repeat(b []byte, count int) []byte

 

 

6、子串

// 判斷 s 是否有前綴 prefix(后綴 suffix)
func HasPrefix(s, prefix []byte) bool
func HasSuffix(s, suffix []byte) bool

// 判斷 b 中是否包含子串 subslice(字符 r)
func Contains(b, subslice []byte) bool
func ContainsRune(b []byte, r rune) bool

// 判斷 b 中是否包含 chars 中的任何一個字符
func ContainsAny(b []byte, chars string) bool

// 查找子串 sep(字節 c、字符 r)在 s 中第一次出現的位置,找不到則返回 -1。
func Index(s, sep []byte) int
func IndexByte(s []byte, c byte) int
func IndexRune(s []byte, r rune) int

// 查找 chars 中的任何一個字符在 s 中第一次出現的位置,找不到則返回 -1。
func IndexAny(s []byte, chars string) int

// 查找符合 f 的字符在 s 中第一次出現的位置,找不到則返回 -1。
func IndexFunc(s []byte, f func(r rune) bool) int

// 功能同上,只不過查找最后一次出現的位置。
func LastIndex(s, sep []byte) int
func LastIndexByte(s []byte, c byte) int
func LastIndexAny(s []byte, chars string) int
func LastIndexFunc(s []byte, f func(r rune) bool) int

// 獲取 sep 在 s 中出現的次數(sep 不能重疊)。
func Count(s, sep []byte) int

 

7、

func NewReader(b []byte) *Reader
  • NewReader創建一個從s讀取數據的Reader。

 

 

buffer包

bytes.buffer是一個緩沖byte類型的緩沖器,這個緩沖器里存放着都是byte。

Buffer結構體定義如下:

type Buffer struct {
    buf       []byte   // contents are the bytes buf[off : len(buf)]
    off       int      // read at &buf[off], write at &buf[len(buf)]
    bootstrap [64]byte // memory to hold first slice; helps small buffers avoid allocation.
    lastRead  readOp   // last read operation, so that Unread* can work correctly.
}

 

buffer上的操作:

1、初始化buffer

func NewBuffer(buf []byte) *Buffer { return &Buffer{buf: buf} }
func NewBufferString(s string) *Buffer { return &Buffer{buf: []byte(s)} }
  • NewBuffer方法將 buf 包裝成 bytes.Buffer 對象;
  • NewBufferString方法將string轉換為byte之后,包裝成bytes.Buffer對象;

 

 

2、讀buffer

func (b *Buffer) Read(p []byte) (n int, err error)
func (b *Buffer) ReadByte() (c byte, err error)
func (b *Buffer) ReadRune() (r rune, size int, err error)
func (b *Buffer) ReadBytes(delim byte) (line []byte, err error)
func (b *Buffer) ReadString(delim byte) (line string, err error)
func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)
func (b *Buffer) Next(n int) []byte
  • Read方法將緩存器buf[b.off:]的內容讀到參數p中,緩沖器相應的減少了,返回的n為成功讀的數量;
  • ReadByte方法返回一個字節;
  • ReadRune方法定義了如何讀取Buffer中UTF8編碼的rune數據;
  • ReadBytes和ReadString方法讀取Buffer中從off到第一次delim之間的數據,並且包括delim;
  • Next方法讀取前 n 字節的數據並以切片形式返回,如果數據長度小於 n,則全部讀取。

 

 

3、寫buffer

func (b *Buffer) Write(p []byte) (n int, err error)
func (b *Buffer) WriteString(s string) (n int, err error)
func (b *Buffer) WriteByte(c byte) error
func (b *Buffer) WriteRune(r rune) (n int, err error)
func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)
  • 使用Write方法,將一個byte類型的slice放到緩沖器的尾部;
  • 使用WriteString方法,將一個字符串放到緩沖器的尾部;
  • 使用WriteByte方法,將一個byte類型的數據放到緩沖器的尾部;
  • 使用WriteRune方法,將一個rune類型的數據放到緩沖器的尾部;
  • 使用WriteTo方法,將一個緩沖器的數據寫到w里,w是實現io.Writer的,比如os.File就是實現io.Writer;

 

 

 

 

binary包

 

func Read(r io.Reader, order ByteOrder, data interface{}) error
func Write(w io.Writer, order ByteOrder, data interface{}) error
  • Read方法從r中讀取binary編碼的數據並賦給data,data必須是一個指向定長值的指針或者定長值的切片。從r讀取的字節使用order指定的字節序解碼並寫入data的字段里當寫入結構體是,名字中有'_'的字段會被跳過,這些字段可用於填充(內存空間)。
  • Write方法將data的binary編碼格式寫入w,data必須是定長值、定長值的切片、定長值的指針。order指定寫入數據的字節序,寫入結構體時,名字中有'_'的字段會置為0。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM