| | |
| | |
| | |
| |
|
| | package bytes_test |
| |
|
| | import ( |
| | "bytes" |
| | "encoding/base64" |
| | "fmt" |
| | "io" |
| | "os" |
| | "slices" |
| | "strconv" |
| | "unicode" |
| | ) |
| |
|
| | func ExampleBuffer() { |
| | var b bytes.Buffer |
| | b.Write([]byte("Hello ")) |
| | fmt.Fprintf(&b, "world!") |
| | b.WriteTo(os.Stdout) |
| | |
| | } |
| |
|
| | func ExampleBuffer_reader() { |
| | |
| | buf := bytes.NewBufferString("R29waGVycyBydWxlIQ==") |
| | dec := base64.NewDecoder(base64.StdEncoding, buf) |
| | io.Copy(os.Stdout, dec) |
| | |
| | } |
| |
|
| | func ExampleBuffer_Bytes() { |
| | buf := bytes.Buffer{} |
| | buf.Write([]byte{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'}) |
| | os.Stdout.Write(buf.Bytes()) |
| | |
| | } |
| |
|
| | func ExampleBuffer_AvailableBuffer() { |
| | var buf bytes.Buffer |
| | for i := 0; i < 4; i++ { |
| | b := buf.AvailableBuffer() |
| | b = strconv.AppendInt(b, int64(i), 10) |
| | b = append(b, ' ') |
| | buf.Write(b) |
| | } |
| | os.Stdout.Write(buf.Bytes()) |
| | |
| | } |
| |
|
| | func ExampleBuffer_Cap() { |
| | buf1 := bytes.NewBuffer(make([]byte, 10)) |
| | buf2 := bytes.NewBuffer(make([]byte, 0, 10)) |
| | fmt.Println(buf1.Cap()) |
| | fmt.Println(buf2.Cap()) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleBuffer_Grow() { |
| | var b bytes.Buffer |
| | b.Grow(64) |
| | bb := b.Bytes() |
| | b.Write([]byte("64 bytes or fewer")) |
| | fmt.Printf("%q", bb[:b.Len()]) |
| | |
| | } |
| |
|
| | func ExampleBuffer_Len() { |
| | var b bytes.Buffer |
| | b.Grow(64) |
| | b.Write([]byte("abcde")) |
| | fmt.Printf("%d", b.Len()) |
| | |
| | } |
| |
|
| | func ExampleBuffer_Next() { |
| | var b bytes.Buffer |
| | b.Grow(64) |
| | b.Write([]byte("abcde")) |
| | fmt.Printf("%s\n", b.Next(2)) |
| | fmt.Printf("%s\n", b.Next(2)) |
| | fmt.Printf("%s", b.Next(2)) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleBuffer_Read() { |
| | var b bytes.Buffer |
| | b.Grow(64) |
| | b.Write([]byte("abcde")) |
| | rdbuf := make([]byte, 1) |
| | n, err := b.Read(rdbuf) |
| | if err != nil { |
| | panic(err) |
| | } |
| | fmt.Println(n) |
| | fmt.Println(b.String()) |
| | fmt.Println(string(rdbuf)) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleBuffer_ReadByte() { |
| | var b bytes.Buffer |
| | b.Grow(64) |
| | b.Write([]byte("abcde")) |
| | c, err := b.ReadByte() |
| | if err != nil { |
| | panic(err) |
| | } |
| | fmt.Println(c) |
| | fmt.Println(b.String()) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleClone() { |
| | b := []byte("abc") |
| | clone := bytes.Clone(b) |
| | fmt.Printf("%s\n", clone) |
| | clone[0] = 'd' |
| | fmt.Printf("%s\n", b) |
| | fmt.Printf("%s\n", clone) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleCompare() { |
| | |
| | var a, b []byte |
| | if bytes.Compare(a, b) < 0 { |
| | |
| | } |
| | if bytes.Compare(a, b) <= 0 { |
| | |
| | } |
| | if bytes.Compare(a, b) > 0 { |
| | |
| | } |
| | if bytes.Compare(a, b) >= 0 { |
| | |
| | } |
| |
|
| | |
| | if bytes.Equal(a, b) { |
| | |
| | } |
| | if !bytes.Equal(a, b) { |
| | |
| | } |
| | } |
| |
|
| | func ExampleCompare_search() { |
| | |
| | var needle []byte |
| | var haystack [][]byte |
| | _, found := slices.BinarySearchFunc(haystack, needle, bytes.Compare) |
| | if found { |
| | |
| | } |
| | } |
| |
|
| | func ExampleContains() { |
| | 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(""))) |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleContainsAny() { |
| | 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(""), "")) |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleContainsRune() { |
| | 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(""), '@')) |
| | |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleContainsFunc() { |
| | f := func(r rune) bool { |
| | return r >= 'a' && r <= 'z' |
| | } |
| | fmt.Println(bytes.ContainsFunc([]byte("HELLO"), f)) |
| | fmt.Println(bytes.ContainsFunc([]byte("World"), f)) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleCount() { |
| | fmt.Println(bytes.Count([]byte("cheese"), []byte("e"))) |
| | fmt.Println(bytes.Count([]byte("five"), []byte(""))) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleCut() { |
| | show := func(s, sep string) { |
| | before, after, found := bytes.Cut([]byte(s), []byte(sep)) |
| | fmt.Printf("Cut(%q, %q) = %q, %q, %v\n", s, sep, before, after, found) |
| | } |
| | show("Gopher", "Go") |
| | show("Gopher", "ph") |
| | show("Gopher", "er") |
| | show("Gopher", "Badger") |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleCutPrefix() { |
| | show := func(s, prefix string) { |
| | after, found := bytes.CutPrefix([]byte(s), []byte(prefix)) |
| | fmt.Printf("CutPrefix(%q, %q) = %q, %v\n", s, prefix, after, found) |
| | } |
| | show("Gopher", "Go") |
| | show("Gopher", "ph") |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleCutSuffix() { |
| | show := func(s, suffix string) { |
| | before, found := bytes.CutSuffix([]byte(s), []byte(suffix)) |
| | fmt.Printf("CutSuffix(%q, %q) = %q, %v\n", s, suffix, before, found) |
| | } |
| | show("Gopher", "Go") |
| | show("Gopher", "er") |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleEqual() { |
| | fmt.Println(bytes.Equal([]byte("Go"), []byte("Go"))) |
| | fmt.Println(bytes.Equal([]byte("Go"), []byte("C++"))) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleEqualFold() { |
| | fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go"))) |
| | |
| | } |
| |
|
| | func ExampleFields() { |
| | fmt.Printf("Fields are: %q", bytes.Fields([]byte(" foo bar baz "))) |
| | |
| | } |
| |
|
| | func ExampleFieldsFunc() { |
| | f := func(c rune) bool { |
| | return !unicode.IsLetter(c) && !unicode.IsNumber(c) |
| | } |
| | fmt.Printf("Fields are: %q", bytes.FieldsFunc([]byte(" foo1;bar2,baz3..."), f)) |
| | |
| | } |
| |
|
| | func ExampleHasPrefix() { |
| | fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("Go"))) |
| | fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("C"))) |
| | fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte(""))) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleHasSuffix() { |
| | 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(""))) |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleIndex() { |
| | fmt.Println(bytes.Index([]byte("chicken"), []byte("ken"))) |
| | fmt.Println(bytes.Index([]byte("chicken"), []byte("dmr"))) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleIndexByte() { |
| | fmt.Println(bytes.IndexByte([]byte("chicken"), byte('k'))) |
| | fmt.Println(bytes.IndexByte([]byte("chicken"), byte('g'))) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleIndexFunc() { |
| | 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)) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleIndexAny() { |
| | fmt.Println(bytes.IndexAny([]byte("chicken"), "aeiouy")) |
| | fmt.Println(bytes.IndexAny([]byte("crwth"), "aeiouy")) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleIndexRune() { |
| | fmt.Println(bytes.IndexRune([]byte("chicken"), 'k')) |
| | fmt.Println(bytes.IndexRune([]byte("chicken"), 'd')) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleJoin() { |
| | s := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")} |
| | fmt.Printf("%s", bytes.Join(s, []byte(", "))) |
| | |
| | } |
| |
|
| | func ExampleLastIndex() { |
| | 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"))) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleLastIndexAny() { |
| | fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "MüQp")) |
| | fmt.Println(bytes.LastIndexAny([]byte("go 地鼠"), "地大")) |
| | fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "z,!.")) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleLastIndexByte() { |
| | 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'))) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleLastIndexFunc() { |
| | 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)) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleMap() { |
| | 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\n", bytes.Map(rot13, []byte("'Twas brillig and the slithy gopher..."))) |
| | |
| | |
| | } |
| |
|
| | func ExampleReader_Len() { |
| | fmt.Println(bytes.NewReader([]byte("Hi!")).Len()) |
| | fmt.Println(bytes.NewReader([]byte("こんにちは!")).Len()) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRepeat() { |
| | fmt.Printf("ba%s", bytes.Repeat([]byte("na"), 2)) |
| | |
| | } |
| |
|
| | func ExampleReplace() { |
| | 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)) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleReplaceAll() { |
| | fmt.Printf("%s\n", bytes.ReplaceAll([]byte("oink oink oink"), []byte("oink"), []byte("moo"))) |
| | |
| | |
| | } |
| |
|
| | func ExampleRunes() { |
| | rs := bytes.Runes([]byte("go gopher")) |
| | for _, r := range rs { |
| | fmt.Printf("%#U\n", r) |
| | } |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleSplit() { |
| | 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"))) |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleSplitN() { |
| | 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) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleSplitAfter() { |
| | fmt.Printf("%q\n", bytes.SplitAfter([]byte("a,b,c"), []byte(","))) |
| | |
| | } |
| |
|
| | func ExampleSplitAfterN() { |
| | fmt.Printf("%q\n", bytes.SplitAfterN([]byte("a,b,c"), []byte(","), 2)) |
| | |
| | } |
| |
|
| | func ExampleTitle() { |
| | fmt.Printf("%s", bytes.Title([]byte("her royal highness"))) |
| | |
| | } |
| |
|
| | func ExampleToTitle() { |
| | fmt.Printf("%s\n", bytes.ToTitle([]byte("loud noises"))) |
| | fmt.Printf("%s\n", bytes.ToTitle([]byte("брат"))) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleToTitleSpecial() { |
| | str := []byte("ahoj vývojári golang") |
| | totitle := bytes.ToTitleSpecial(unicode.AzeriCase, str) |
| | fmt.Println("Original : " + string(str)) |
| | fmt.Println("ToTitle : " + string(totitle)) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleToValidUTF8() { |
| | fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("abc"), []byte("\uFFFD"))) |
| | fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("a\xffb\xC0\xAFc\xff"), []byte(""))) |
| | fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("\xed\xa0\x80"), []byte("abc"))) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleTrim() { |
| | fmt.Printf("[%q]", bytes.Trim([]byte(" !!! Achtung! Achtung! !!! "), "! ")) |
| | |
| | } |
| |
|
| | func ExampleTrimFunc() { |
| | 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))) |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleTrimLeft() { |
| | fmt.Print(string(bytes.TrimLeft([]byte("453gopher8257"), "0123456789"))) |
| | |
| | |
| | } |
| |
|
| | func ExampleTrimLeftFunc() { |
| | 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))) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleTrimPrefix() { |
| | var b = []byte("Goodbye,, world!") |
| | b = bytes.TrimPrefix(b, []byte("Goodbye,")) |
| | b = bytes.TrimPrefix(b, []byte("See ya,")) |
| | fmt.Printf("Hello%s", b) |
| | |
| | } |
| |
|
| | func ExampleTrimSpace() { |
| | fmt.Printf("%s", bytes.TrimSpace([]byte(" \t\n a lone gopher \n\t\r\n"))) |
| | |
| | } |
| |
|
| | func ExampleTrimSuffix() { |
| | 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) |
| | |
| | } |
| |
|
| | func ExampleTrimRight() { |
| | fmt.Print(string(bytes.TrimRight([]byte("453gopher8257"), "0123456789"))) |
| | |
| | |
| | } |
| |
|
| | func ExampleTrimRightFunc() { |
| | 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))) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleToLower() { |
| | fmt.Printf("%s", bytes.ToLower([]byte("Gopher"))) |
| | |
| | } |
| |
|
| | func ExampleToLowerSpecial() { |
| | str := []byte("AHOJ VÝVOJÁRİ GOLANG") |
| | totitle := bytes.ToLowerSpecial(unicode.AzeriCase, str) |
| | fmt.Println("Original : " + string(str)) |
| | fmt.Println("ToLower : " + string(totitle)) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleToUpper() { |
| | fmt.Printf("%s", bytes.ToUpper([]byte("Gopher"))) |
| | |
| | } |
| |
|
| | func ExampleToUpperSpecial() { |
| | str := []byte("ahoj vývojári golang") |
| | totitle := bytes.ToUpperSpecial(unicode.AzeriCase, str) |
| | fmt.Println("Original : " + string(str)) |
| | fmt.Println("ToUpper : " + string(totitle)) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleLines() { |
| | text := []byte("Hello\nWorld\nGo Programming\n") |
| | for line := range bytes.Lines(text) { |
| | fmt.Printf("%q\n", line) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleSplitSeq() { |
| | s := []byte("a,b,c,d") |
| | for part := range bytes.SplitSeq(s, []byte(",")) { |
| | fmt.Printf("%q\n", part) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleSplitAfterSeq() { |
| | s := []byte("a,b,c,d") |
| | for part := range bytes.SplitAfterSeq(s, []byte(",")) { |
| | fmt.Printf("%q\n", part) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleFieldsSeq() { |
| | text := []byte("The quick brown fox") |
| | fmt.Println("Split byte slice into fields:") |
| | for word := range bytes.FieldsSeq(text) { |
| | fmt.Printf("%q\n", word) |
| | } |
| |
|
| | textWithSpaces := []byte(" lots of spaces ") |
| | fmt.Println("\nSplit byte slice with multiple spaces:") |
| | for word := range bytes.FieldsSeq(textWithSpaces) { |
| | fmt.Printf("%q\n", word) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleFieldsFuncSeq() { |
| | text := []byte("The quick brown fox") |
| | fmt.Println("Split on whitespace(similar to FieldsSeq):") |
| | for word := range bytes.FieldsFuncSeq(text, unicode.IsSpace) { |
| | fmt.Printf("%q\n", word) |
| | } |
| |
|
| | mixedText := []byte("abc123def456ghi") |
| | fmt.Println("\nSplit on digits:") |
| | for word := range bytes.FieldsFuncSeq(mixedText, unicode.IsDigit) { |
| | fmt.Printf("%q\n", word) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|