Source file src/bytes/example_test.go

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package bytes_test
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/base64"
    10  	"fmt"
    11  	"io"
    12  	"os"
    13  	"sort"
    14  	"strconv"
    15  	"unicode"
    16  )
    17  
    18  func ExampleBuffer() {
    19  	var b bytes.Buffer // A Buffer needs no initialization.
    20  	b.Write([]byte("Hello "))
    21  	fmt.Fprintf(&b, "world!")
    22  	b.WriteTo(os.Stdout)
    23  	// Output: Hello world!
    24  }
    25  
    26  func ExampleBuffer_reader() {
    27  	// A Buffer can turn a string or a []byte into an io.Reader.
    28  	buf := bytes.NewBufferString("R29waGVycyBydWxlIQ==")
    29  	dec := base64.NewDecoder(base64.StdEncoding, buf)
    30  	io.Copy(os.Stdout, dec)
    31  	// Output: Gophers rule!
    32  }
    33  
    34  func ExampleBuffer_Bytes() {
    35  	buf := bytes.Buffer{}
    36  	buf.Write([]byte{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'})
    37  	os.Stdout.Write(buf.Bytes())
    38  	// Output: hello world
    39  }
    40  
    41  func ExampleBuffer_AvailableBuffer() {
    42  	var buf bytes.Buffer
    43  	for i := 0; i < 4; i++ {
    44  		b := buf.AvailableBuffer()
    45  		b = strconv.AppendInt(b, int64(i), 10)
    46  		b = append(b, ' ')
    47  		buf.Write(b)
    48  	}
    49  	os.Stdout.Write(buf.Bytes())
    50  	// Output: 0 1 2 3
    51  }
    52  
    53  func ExampleBuffer_Cap() {
    54  	buf1 := bytes.NewBuffer(make([]byte, 10))
    55  	buf2 := bytes.NewBuffer(make([]byte, 0, 10))
    56  	fmt.Println(buf1.Cap())
    57  	fmt.Println(buf2.Cap())
    58  	// Output:
    59  	// 10
    60  	// 10
    61  }
    62  
    63  func ExampleBuffer_Grow() {
    64  	var b bytes.Buffer
    65  	b.Grow(64)
    66  	bb := b.Bytes()
    67  	b.Write([]byte("64 bytes or fewer"))
    68  	fmt.Printf("%q", bb[:b.Len()])
    69  	// Output: "64 bytes or fewer"
    70  }
    71  
    72  func ExampleBuffer_Len() {
    73  	var b bytes.Buffer
    74  	b.Grow(64)
    75  	b.Write([]byte("abcde"))
    76  	fmt.Printf("%d", b.Len())
    77  	// Output: 5
    78  }
    79  
    80  func ExampleBuffer_Next() {
    81  	var b bytes.Buffer
    82  	b.Grow(64)
    83  	b.Write([]byte("abcde"))
    84  	fmt.Printf("%s\n", b.Next(2))
    85  	fmt.Printf("%s\n", b.Next(2))
    86  	fmt.Printf("%s", b.Next(2))
    87  	// Output:
    88  	// ab
    89  	// cd
    90  	// e
    91  }
    92  
    93  func ExampleBuffer_Read() {
    94  	var b bytes.Buffer
    95  	b.Grow(64)
    96  	b.Write([]byte("abcde"))
    97  	rdbuf := make([]byte, 1)
    98  	n, err := b.Read(rdbuf)
    99  	if err != nil {
   100  		panic(err)
   101  	}
   102  	fmt.Println(n)
   103  	fmt.Println(b.String())
   104  	fmt.Println(string(rdbuf))
   105  	// Output
   106  	// 1
   107  	// bcde
   108  	// a
   109  }
   110  
   111  func ExampleBuffer_ReadByte() {
   112  	var b bytes.Buffer
   113  	b.Grow(64)
   114  	b.Write([]byte("abcde"))
   115  	c, err := b.ReadByte()
   116  	if err != nil {
   117  		panic(err)
   118  	}
   119  	fmt.Println(c)
   120  	fmt.Println(b.String())
   121  	// Output
   122  	// 97
   123  	// bcde
   124  }
   125  
   126  func ExampleClone() {
   127  	b := []byte("abc")
   128  	clone := bytes.Clone(b)
   129  	fmt.Printf("%s\n", clone)
   130  	clone[0] = 'd'
   131  	fmt.Printf("%s\n", b)
   132  	fmt.Printf("%s\n", clone)
   133  	// Output:
   134  	// abc
   135  	// abc
   136  	// dbc
   137  }
   138  
   139  func ExampleCompare() {
   140  	// Interpret Compare's result by comparing it to zero.
   141  	var a, b []byte
   142  	if bytes.Compare(a, b) < 0 {
   143  		// a less b
   144  	}
   145  	if bytes.Compare(a, b) <= 0 {
   146  		// a less or equal b
   147  	}
   148  	if bytes.Compare(a, b) > 0 {
   149  		// a greater b
   150  	}
   151  	if bytes.Compare(a, b) >= 0 {
   152  		// a greater or equal b
   153  	}
   154  
   155  	// Prefer Equal to Compare for equality comparisons.
   156  	if bytes.Equal(a, b) {
   157  		// a equal b
   158  	}
   159  	if !bytes.Equal(a, b) {
   160  		// a not equal b
   161  	}
   162  }
   163  
   164  func ExampleCompare_search() {
   165  	// Binary search to find a matching byte slice.
   166  	var needle []byte
   167  	var haystack [][]byte // Assume sorted
   168  	i := sort.Search(len(haystack), func(i int) bool {
   169  		// Return haystack[i] >= needle.
   170  		return bytes.Compare(haystack[i], needle) >= 0
   171  	})
   172  	if i < len(haystack) && bytes.Equal(haystack[i], needle) {
   173  		// Found it!
   174  	}
   175  }
   176  
   177  func ExampleContains() {
   178  	fmt.Println(bytes.Contains([]byte("seafood"), []byte("foo")))
   179  	fmt.Println(bytes.Contains([]byte("seafood"), []byte("bar")))
   180  	fmt.Println(bytes.Contains([]byte("seafood"), []byte("")))
   181  	fmt.Println(bytes.Contains([]byte(""), []byte("")))
   182  	// Output:
   183  	// true
   184  	// false
   185  	// true
   186  	// true
   187  }
   188  
   189  func ExampleContainsAny() {
   190  	fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "fÄo!"))
   191  	fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "去是伟大的."))
   192  	fmt.Println(bytes.ContainsAny([]byte("I like seafood."), ""))
   193  	fmt.Println(bytes.ContainsAny([]byte(""), ""))
   194  	// Output:
   195  	// true
   196  	// true
   197  	// false
   198  	// false
   199  }
   200  
   201  func ExampleContainsRune() {
   202  	fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'f'))
   203  	fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'ö'))
   204  	fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '大'))
   205  	fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '!'))
   206  	fmt.Println(bytes.ContainsRune([]byte(""), '@'))
   207  	// Output:
   208  	// true
   209  	// false
   210  	// true
   211  	// true
   212  	// false
   213  }
   214  
   215  func ExampleContainsFunc() {
   216  	f := func(r rune) bool {
   217  		return r >= 'a' && r <= 'z'
   218  	}
   219  	fmt.Println(bytes.ContainsFunc([]byte("HELLO"), f))
   220  	fmt.Println(bytes.ContainsFunc([]byte("World"), f))
   221  	// Output:
   222  	// false
   223  	// true
   224  }
   225  
   226  func ExampleCount() {
   227  	fmt.Println(bytes.Count([]byte("cheese"), []byte("e")))
   228  	fmt.Println(bytes.Count([]byte("five"), []byte(""))) // before & after each rune
   229  	// Output:
   230  	// 3
   231  	// 5
   232  }
   233  
   234  func ExampleCut() {
   235  	show := func(s, sep string) {
   236  		before, after, found := bytes.Cut([]byte(s), []byte(sep))
   237  		fmt.Printf("Cut(%q, %q) = %q, %q, %v\n", s, sep, before, after, found)
   238  	}
   239  	show("Gopher", "Go")
   240  	show("Gopher", "ph")
   241  	show("Gopher", "er")
   242  	show("Gopher", "Badger")
   243  	// Output:
   244  	// Cut("Gopher", "Go") = "", "pher", true
   245  	// Cut("Gopher", "ph") = "Go", "er", true
   246  	// Cut("Gopher", "er") = "Goph", "", true
   247  	// Cut("Gopher", "Badger") = "Gopher", "", false
   248  }
   249  
   250  func ExampleCutPrefix() {
   251  	show := func(s, sep string) {
   252  		after, found := bytes.CutPrefix([]byte(s), []byte(sep))
   253  		fmt.Printf("CutPrefix(%q, %q) = %q, %v\n", s, sep, after, found)
   254  	}
   255  	show("Gopher", "Go")
   256  	show("Gopher", "ph")
   257  	// Output:
   258  	// CutPrefix("Gopher", "Go") = "pher", true
   259  	// CutPrefix("Gopher", "ph") = "Gopher", false
   260  }
   261  
   262  func ExampleCutSuffix() {
   263  	show := func(s, sep string) {
   264  		before, found := bytes.CutSuffix([]byte(s), []byte(sep))
   265  		fmt.Printf("CutSuffix(%q, %q) = %q, %v\n", s, sep, before, found)
   266  	}
   267  	show("Gopher", "Go")
   268  	show("Gopher", "er")
   269  	// Output:
   270  	// CutSuffix("Gopher", "Go") = "Gopher", false
   271  	// CutSuffix("Gopher", "er") = "Goph", true
   272  }
   273  
   274  func ExampleEqual() {
   275  	fmt.Println(bytes.Equal([]byte("Go"), []byte("Go")))
   276  	fmt.Println(bytes.Equal([]byte("Go"), []byte("C++")))
   277  	// Output:
   278  	// true
   279  	// false
   280  }
   281  
   282  func ExampleEqualFold() {
   283  	fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go")))
   284  	// Output: true
   285  }
   286  
   287  func ExampleFields() {
   288  	fmt.Printf("Fields are: %q", bytes.Fields([]byte("  foo bar  baz   ")))
   289  	// Output: Fields are: ["foo" "bar" "baz"]
   290  }
   291  
   292  func ExampleFieldsFunc() {
   293  	f := func(c rune) bool {
   294  		return !unicode.IsLetter(c) && !unicode.IsNumber(c)
   295  	}
   296  	fmt.Printf("Fields are: %q", bytes.FieldsFunc([]byte("  foo1;bar2,baz3..."), f))
   297  	// Output: Fields are: ["foo1" "bar2" "baz3"]
   298  }
   299  
   300  func ExampleHasPrefix() {
   301  	fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("Go")))
   302  	fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("C")))
   303  	fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("")))
   304  	// Output:
   305  	// true
   306  	// false
   307  	// true
   308  }
   309  
   310  func ExampleHasSuffix() {
   311  	fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("go")))
   312  	fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("O")))
   313  	fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("Ami")))
   314  	fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("")))
   315  	// Output:
   316  	// true
   317  	// false
   318  	// false
   319  	// true
   320  }
   321  
   322  func ExampleIndex() {
   323  	fmt.Println(bytes.Index([]byte("chicken"), []byte("ken")))
   324  	fmt.Println(bytes.Index([]byte("chicken"), []byte("dmr")))
   325  	// Output:
   326  	// 4
   327  	// -1
   328  }
   329  
   330  func ExampleIndexByte() {
   331  	fmt.Println(bytes.IndexByte([]byte("chicken"), byte('k')))
   332  	fmt.Println(bytes.IndexByte([]byte("chicken"), byte('g')))
   333  	// Output:
   334  	// 4
   335  	// -1
   336  }
   337  
   338  func ExampleIndexFunc() {
   339  	f := func(c rune) bool {
   340  		return unicode.Is(unicode.Han, c)
   341  	}
   342  	fmt.Println(bytes.IndexFunc([]byte("Hello, 世界"), f))
   343  	fmt.Println(bytes.IndexFunc([]byte("Hello, world"), f))
   344  	// Output:
   345  	// 7
   346  	// -1
   347  }
   348  
   349  func ExampleIndexAny() {
   350  	fmt.Println(bytes.IndexAny([]byte("chicken"), "aeiouy"))
   351  	fmt.Println(bytes.IndexAny([]byte("crwth"), "aeiouy"))
   352  	// Output:
   353  	// 2
   354  	// -1
   355  }
   356  
   357  func ExampleIndexRune() {
   358  	fmt.Println(bytes.IndexRune([]byte("chicken"), 'k'))
   359  	fmt.Println(bytes.IndexRune([]byte("chicken"), 'd'))
   360  	// Output:
   361  	// 4
   362  	// -1
   363  }
   364  
   365  func ExampleJoin() {
   366  	s := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}
   367  	fmt.Printf("%s", bytes.Join(s, []byte(", ")))
   368  	// Output: foo, bar, baz
   369  }
   370  
   371  func ExampleLastIndex() {
   372  	fmt.Println(bytes.Index([]byte("go gopher"), []byte("go")))
   373  	fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("go")))
   374  	fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("rodent")))
   375  	// Output:
   376  	// 0
   377  	// 3
   378  	// -1
   379  }
   380  
   381  func ExampleLastIndexAny() {
   382  	fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "MüQp"))
   383  	fmt.Println(bytes.LastIndexAny([]byte("go 地鼠"), "地大"))
   384  	fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "z,!."))
   385  	// Output:
   386  	// 5
   387  	// 3
   388  	// -1
   389  }
   390  
   391  func ExampleLastIndexByte() {
   392  	fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('g')))
   393  	fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('r')))
   394  	fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('z')))
   395  	// Output:
   396  	// 3
   397  	// 8
   398  	// -1
   399  }
   400  
   401  func ExampleLastIndexFunc() {
   402  	fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsLetter))
   403  	fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsPunct))
   404  	fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsNumber))
   405  	// Output:
   406  	// 8
   407  	// 9
   408  	// -1
   409  }
   410  
   411  func ExampleMap() {
   412  	rot13 := func(r rune) rune {
   413  		switch {
   414  		case r >= 'A' && r <= 'Z':
   415  			return 'A' + (r-'A'+13)%26
   416  		case r >= 'a' && r <= 'z':
   417  			return 'a' + (r-'a'+13)%26
   418  		}
   419  		return r
   420  	}
   421  	fmt.Printf("%s\n", bytes.Map(rot13, []byte("'Twas brillig and the slithy gopher...")))
   422  	// Output:
   423  	// 'Gjnf oevyyvt naq gur fyvgul tbcure...
   424  }
   425  
   426  func ExampleReader_Len() {
   427  	fmt.Println(bytes.NewReader([]byte("Hi!")).Len())
   428  	fmt.Println(bytes.NewReader([]byte("こんにちは!")).Len())
   429  	// Output:
   430  	// 3
   431  	// 16
   432  }
   433  
   434  func ExampleRepeat() {
   435  	fmt.Printf("ba%s", bytes.Repeat([]byte("na"), 2))
   436  	// Output: banana
   437  }
   438  
   439  func ExampleReplace() {
   440  	fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("k"), []byte("ky"), 2))
   441  	fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("oink"), []byte("moo"), -1))
   442  	// Output:
   443  	// oinky oinky oink
   444  	// moo moo moo
   445  }
   446  
   447  func ExampleReplaceAll() {
   448  	fmt.Printf("%s\n", bytes.ReplaceAll([]byte("oink oink oink"), []byte("oink"), []byte("moo")))
   449  	// Output:
   450  	// moo moo moo
   451  }
   452  
   453  func ExampleRunes() {
   454  	rs := bytes.Runes([]byte("go gopher"))
   455  	for _, r := range rs {
   456  		fmt.Printf("%#U\n", r)
   457  	}
   458  	// Output:
   459  	// U+0067 'g'
   460  	// U+006F 'o'
   461  	// U+0020 ' '
   462  	// U+0067 'g'
   463  	// U+006F 'o'
   464  	// U+0070 'p'
   465  	// U+0068 'h'
   466  	// U+0065 'e'
   467  	// U+0072 'r'
   468  }
   469  
   470  func ExampleSplit() {
   471  	fmt.Printf("%q\n", bytes.Split([]byte("a,b,c"), []byte(",")))
   472  	fmt.Printf("%q\n", bytes.Split([]byte("a man a plan a canal panama"), []byte("a ")))
   473  	fmt.Printf("%q\n", bytes.Split([]byte(" xyz "), []byte("")))
   474  	fmt.Printf("%q\n", bytes.Split([]byte(""), []byte("Bernardo O'Higgins")))
   475  	// Output:
   476  	// ["a" "b" "c"]
   477  	// ["" "man " "plan " "canal panama"]
   478  	// [" " "x" "y" "z" " "]
   479  	// [""]
   480  }
   481  
   482  func ExampleSplitN() {
   483  	fmt.Printf("%q\n", bytes.SplitN([]byte("a,b,c"), []byte(","), 2))
   484  	z := bytes.SplitN([]byte("a,b,c"), []byte(","), 0)
   485  	fmt.Printf("%q (nil = %v)\n", z, z == nil)
   486  	// Output:
   487  	// ["a" "b,c"]
   488  	// [] (nil = true)
   489  }
   490  
   491  func ExampleSplitAfter() {
   492  	fmt.Printf("%q\n", bytes.SplitAfter([]byte("a,b,c"), []byte(",")))
   493  	// Output: ["a," "b," "c"]
   494  }
   495  
   496  func ExampleSplitAfterN() {
   497  	fmt.Printf("%q\n", bytes.SplitAfterN([]byte("a,b,c"), []byte(","), 2))
   498  	// Output: ["a," "b,c"]
   499  }
   500  
   501  func ExampleTitle() {
   502  	fmt.Printf("%s", bytes.Title([]byte("her royal highness")))
   503  	// Output: Her Royal Highness
   504  }
   505  
   506  func ExampleToTitle() {
   507  	fmt.Printf("%s\n", bytes.ToTitle([]byte("loud noises")))
   508  	fmt.Printf("%s\n", bytes.ToTitle([]byte("хлеб")))
   509  	// Output:
   510  	// LOUD NOISES
   511  	// ХЛЕБ
   512  }
   513  
   514  func ExampleToTitleSpecial() {
   515  	str := []byte("ahoj vývojári golang")
   516  	totitle := bytes.ToTitleSpecial(unicode.AzeriCase, str)
   517  	fmt.Println("Original : " + string(str))
   518  	fmt.Println("ToTitle : " + string(totitle))
   519  	// Output:
   520  	// Original : ahoj vývojári golang
   521  	// ToTitle : AHOJ VÝVOJÁRİ GOLANG
   522  }
   523  
   524  func ExampleToValidUTF8() {
   525  	fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("abc"), []byte("\uFFFD")))
   526  	fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("a\xffb\xC0\xAFc\xff"), []byte("")))
   527  	fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("\xed\xa0\x80"), []byte("abc")))
   528  	// Output:
   529  	// abc
   530  	// abc
   531  	// abc
   532  }
   533  
   534  func ExampleTrim() {
   535  	fmt.Printf("[%q]", bytes.Trim([]byte(" !!! Achtung! Achtung! !!! "), "! "))
   536  	// Output: ["Achtung! Achtung"]
   537  }
   538  
   539  func ExampleTrimFunc() {
   540  	fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsLetter)))
   541  	fmt.Println(string(bytes.TrimFunc([]byte("\"go-gopher!\""), unicode.IsLetter)))
   542  	fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsPunct)))
   543  	fmt.Println(string(bytes.TrimFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
   544  	// Output:
   545  	// -gopher!
   546  	// "go-gopher!"
   547  	// go-gopher
   548  	// go-gopher!
   549  }
   550  
   551  func ExampleTrimLeft() {
   552  	fmt.Print(string(bytes.TrimLeft([]byte("453gopher8257"), "0123456789")))
   553  	// Output:
   554  	// gopher8257
   555  }
   556  
   557  func ExampleTrimLeftFunc() {
   558  	fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher"), unicode.IsLetter)))
   559  	fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher!"), unicode.IsPunct)))
   560  	fmt.Println(string(bytes.TrimLeftFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
   561  	// Output:
   562  	// -gopher
   563  	// go-gopher!
   564  	// go-gopher!567
   565  }
   566  
   567  func ExampleTrimPrefix() {
   568  	var b = []byte("Goodbye,, world!")
   569  	b = bytes.TrimPrefix(b, []byte("Goodbye,"))
   570  	b = bytes.TrimPrefix(b, []byte("See ya,"))
   571  	fmt.Printf("Hello%s", b)
   572  	// Output: Hello, world!
   573  }
   574  
   575  func ExampleTrimSpace() {
   576  	fmt.Printf("%s", bytes.TrimSpace([]byte(" \t\n a lone gopher \n\t\r\n")))
   577  	// Output: a lone gopher
   578  }
   579  
   580  func ExampleTrimSuffix() {
   581  	var b = []byte("Hello, goodbye, etc!")
   582  	b = bytes.TrimSuffix(b, []byte("goodbye, etc!"))
   583  	b = bytes.TrimSuffix(b, []byte("gopher"))
   584  	b = append(b, bytes.TrimSuffix([]byte("world!"), []byte("x!"))...)
   585  	os.Stdout.Write(b)
   586  	// Output: Hello, world!
   587  }
   588  
   589  func ExampleTrimRight() {
   590  	fmt.Print(string(bytes.TrimRight([]byte("453gopher8257"), "0123456789")))
   591  	// Output:
   592  	// 453gopher
   593  }
   594  
   595  func ExampleTrimRightFunc() {
   596  	fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher"), unicode.IsLetter)))
   597  	fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher!"), unicode.IsPunct)))
   598  	fmt.Println(string(bytes.TrimRightFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
   599  	// Output:
   600  	// go-
   601  	// go-gopher
   602  	// 1234go-gopher!
   603  }
   604  
   605  func ExampleToLower() {
   606  	fmt.Printf("%s", bytes.ToLower([]byte("Gopher")))
   607  	// Output: gopher
   608  }
   609  
   610  func ExampleToLowerSpecial() {
   611  	str := []byte("AHOJ VÝVOJÁRİ GOLANG")
   612  	totitle := bytes.ToLowerSpecial(unicode.AzeriCase, str)
   613  	fmt.Println("Original : " + string(str))
   614  	fmt.Println("ToLower : " + string(totitle))
   615  	// Output:
   616  	// Original : AHOJ VÝVOJÁRİ GOLANG
   617  	// ToLower : ahoj vývojári golang
   618  }
   619  
   620  func ExampleToUpper() {
   621  	fmt.Printf("%s", bytes.ToUpper([]byte("Gopher")))
   622  	// Output: GOPHER
   623  }
   624  
   625  func ExampleToUpperSpecial() {
   626  	str := []byte("ahoj vývojári golang")
   627  	totitle := bytes.ToUpperSpecial(unicode.AzeriCase, str)
   628  	fmt.Println("Original : " + string(str))
   629  	fmt.Println("ToUpper : " + string(totitle))
   630  	// Output:
   631  	// Original : ahoj vývojári golang
   632  	// ToUpper : AHOJ VÝVOJÁRİ GOLANG
   633  }
   634  

View as plain text