1
0
mirror of https://github.com/golang/go synced 2024-11-17 14:14:56 -07:00

encoding/base32: Add examples for Encode/Decode

Updates golang/go#37595

Change-Id: I7568e7416d5504e9dc67061c79f66e3a0d597dee
Reviewed-on: https://go-review.googlesource.com/c/go/+/351470
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Trust: David Chase <drchase@google.com>
This commit is contained in:
jiahua wang 2021-09-22 22:32:55 +08:00 committed by Ian Lance Taylor
parent b88a6882a5
commit f9a53b6b4d

View File

@ -20,6 +20,15 @@ func ExampleEncoding_EncodeToString() {
// MFXHSIBLEBXWYZBAEYQGIYLUME======
}
func ExampleEncoding_Encode() {
data := []byte("Hello, world!")
dst := make([]byte, base32.StdEncoding.EncodedLen(len(data)))
base32.StdEncoding.Encode(dst, data)
fmt.Println(string(dst))
// Output:
// JBSWY3DPFQQHO33SNRSCC===
}
func ExampleEncoding_DecodeString() {
str := "ONXW2ZJAMRQXIYJAO5UXI2BAAAQGC3TEEDX3XPY="
data, err := base32.StdEncoding.DecodeString(str)
@ -32,6 +41,20 @@ func ExampleEncoding_DecodeString() {
// "some data with \x00 and \ufeff"
}
func ExampleEncoding_Decode() {
str := "JBSWY3DPFQQHO33SNRSCC==="
dst := make([]byte, base32.StdEncoding.DecodedLen(len(str)))
n, err := base32.StdEncoding.Decode(dst, []byte(str))
if err != nil {
fmt.Println("decode error:", err)
return
}
dst = dst[:n]
fmt.Printf("%q\n", dst)
// Output:
// "Hello, world!"
}
func ExampleNewEncoder() {
input := []byte("foo\x00bar")
encoder := base32.NewEncoder(base32.StdEncoding, os.Stdout)