Source file src/encoding/hex/example_test.go

     1  // Copyright 2016 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 hex_test
     6  
     7  import (
     8  	"encoding/hex"
     9  	"fmt"
    10  	"log"
    11  	"os"
    12  )
    13  
    14  func ExampleEncode() {
    15  	src := []byte("Hello Gopher!")
    16  
    17  	dst := make([]byte, hex.EncodedLen(len(src)))
    18  	hex.Encode(dst, src)
    19  
    20  	fmt.Printf("%s\n", dst)
    21  
    22  	// Output:
    23  	// 48656c6c6f20476f7068657221
    24  }
    25  
    26  func ExampleDecode() {
    27  	src := []byte("48656c6c6f20476f7068657221")
    28  
    29  	dst := make([]byte, hex.DecodedLen(len(src)))
    30  	n, err := hex.Decode(dst, src)
    31  	if err != nil {
    32  		log.Fatal(err)
    33  	}
    34  
    35  	fmt.Printf("%s\n", dst[:n])
    36  
    37  	// Output:
    38  	// Hello Gopher!
    39  }
    40  
    41  func ExampleDecodeString() {
    42  	const s = "48656c6c6f20476f7068657221"
    43  	decoded, err := hex.DecodeString(s)
    44  	if err != nil {
    45  		log.Fatal(err)
    46  	}
    47  
    48  	fmt.Printf("%s\n", decoded)
    49  
    50  	// Output:
    51  	// Hello Gopher!
    52  }
    53  
    54  func ExampleDump() {
    55  	content := []byte("Go is an open source programming language.")
    56  
    57  	fmt.Printf("%s", hex.Dump(content))
    58  
    59  	// Output:
    60  	// 00000000  47 6f 20 69 73 20 61 6e  20 6f 70 65 6e 20 73 6f  |Go is an open so|
    61  	// 00000010  75 72 63 65 20 70 72 6f  67 72 61 6d 6d 69 6e 67  |urce programming|
    62  	// 00000020  20 6c 61 6e 67 75 61 67  65 2e                    | language.|
    63  }
    64  
    65  func ExampleDumper() {
    66  	lines := []string{
    67  		"Go is an open source programming language.",
    68  		"\n",
    69  		"We encourage all Go users to subscribe to golang-announce.",
    70  	}
    71  
    72  	stdoutDumper := hex.Dumper(os.Stdout)
    73  
    74  	defer stdoutDumper.Close()
    75  
    76  	for _, line := range lines {
    77  		stdoutDumper.Write([]byte(line))
    78  	}
    79  
    80  	// Output:
    81  	// 00000000  47 6f 20 69 73 20 61 6e  20 6f 70 65 6e 20 73 6f  |Go is an open so|
    82  	// 00000010  75 72 63 65 20 70 72 6f  67 72 61 6d 6d 69 6e 67  |urce programming|
    83  	// 00000020  20 6c 61 6e 67 75 61 67  65 2e 0a 57 65 20 65 6e  | language..We en|
    84  	// 00000030  63 6f 75 72 61 67 65 20  61 6c 6c 20 47 6f 20 75  |courage all Go u|
    85  	// 00000040  73 65 72 73 20 74 6f 20  73 75 62 73 63 72 69 62  |sers to subscrib|
    86  	// 00000050  65 20 74 6f 20 67 6f 6c  61 6e 67 2d 61 6e 6e 6f  |e to golang-anno|
    87  	// 00000060  75 6e 63 65 2e                                    |unce.|
    88  }
    89  
    90  func ExampleEncodeToString() {
    91  	src := []byte("Hello")
    92  	encodedStr := hex.EncodeToString(src)
    93  
    94  	fmt.Printf("%s\n", encodedStr)
    95  
    96  	// Output:
    97  	// 48656c6c6f
    98  }
    99  

View as plain text