...

Source file src/encoding/base32/example_test.go

Documentation: encoding/base32

     1  // Copyright 2012 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  // Keep in sync with ../base64/example_test.go.
     6  
     7  package base32_test
     8  
     9  import (
    10  	"encoding/base32"
    11  	"fmt"
    12  	"os"
    13  )
    14  
    15  func ExampleEncoding_EncodeToString() {
    16  	data := []byte("any + old & data")
    17  	str := base32.StdEncoding.EncodeToString(data)
    18  	fmt.Println(str)
    19  	// Output:
    20  	// MFXHSIBLEBXWYZBAEYQGIYLUME======
    21  }
    22  
    23  func ExampleEncoding_Encode() {
    24  	data := []byte("Hello, world!")
    25  	dst := make([]byte, base32.StdEncoding.EncodedLen(len(data)))
    26  	base32.StdEncoding.Encode(dst, data)
    27  	fmt.Println(string(dst))
    28  	// Output:
    29  	// JBSWY3DPFQQHO33SNRSCC===
    30  }
    31  
    32  func ExampleEncoding_DecodeString() {
    33  	str := "ONXW2ZJAMRQXIYJAO5UXI2BAAAQGC3TEEDX3XPY="
    34  	data, err := base32.StdEncoding.DecodeString(str)
    35  	if err != nil {
    36  		fmt.Println("error:", err)
    37  		return
    38  	}
    39  	fmt.Printf("%q\n", data)
    40  	// Output:
    41  	// "some data with \x00 and \ufeff"
    42  }
    43  
    44  func ExampleEncoding_Decode() {
    45  	str := "JBSWY3DPFQQHO33SNRSCC==="
    46  	dst := make([]byte, base32.StdEncoding.DecodedLen(len(str)))
    47  	n, err := base32.StdEncoding.Decode(dst, []byte(str))
    48  	if err != nil {
    49  		fmt.Println("decode error:", err)
    50  		return
    51  	}
    52  	dst = dst[:n]
    53  	fmt.Printf("%q\n", dst)
    54  	// Output:
    55  	// "Hello, world!"
    56  }
    57  
    58  func ExampleNewEncoder() {
    59  	input := []byte("foo\x00bar")
    60  	encoder := base32.NewEncoder(base32.StdEncoding, os.Stdout)
    61  	encoder.Write(input)
    62  	// Must close the encoder when finished to flush any partial blocks.
    63  	// If you comment out the following line, the last partial block "r"
    64  	// won't be encoded.
    65  	encoder.Close()
    66  	// Output:
    67  	// MZXW6ADCMFZA====
    68  }
    69  

View as plain text