]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/base{32,64}: add examples.
authorDavid Symonds <dsymonds@golang.org>
Mon, 8 Oct 2012 21:56:34 +0000 (08:56 +1100)
committerDavid Symonds <dsymonds@golang.org>
Mon, 8 Oct 2012 21:56:34 +0000 (08:56 +1100)
Fixes #4136.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/6615061

src/pkg/encoding/base32/example_test.go [new file with mode: 0644]
src/pkg/encoding/base64/example_test.go [new file with mode: 0644]

diff --git a/src/pkg/encoding/base32/example_test.go b/src/pkg/encoding/base32/example_test.go
new file mode 100644 (file)
index 0000000..bd16ca1
--- /dev/null
@@ -0,0 +1,30 @@
+// Copyright 2012 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package base32_test
+
+import (
+       "encoding/base32"
+       "fmt"
+)
+
+func ExampleEncoding_EncodeToString() {
+       data := []byte("any + old & data")
+       str := base32.StdEncoding.EncodeToString(data)
+       fmt.Println(str)
+       // Output:
+       // MFXHSIBLEBXWYZBAEYQGIYLUME======
+}
+
+func ExampleEncoding_DecodeString() {
+       str := "ONXW2ZJAMRQXIYJAO5UXI2BAAAQGC3TEEDX3XPY="
+       data, err := base32.StdEncoding.DecodeString(str)
+       if err != nil {
+               fmt.Println("error:", err)
+               return
+       }
+       fmt.Printf("%q\n", data)
+       // Output:
+       // "some data with \x00 and \ufeff"
+}
diff --git a/src/pkg/encoding/base64/example_test.go b/src/pkg/encoding/base64/example_test.go
new file mode 100644 (file)
index 0000000..40b5438
--- /dev/null
@@ -0,0 +1,30 @@
+// Copyright 2012 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package base64_test
+
+import (
+       "encoding/base64"
+       "fmt"
+)
+
+func ExampleEncoding_EncodeToString() {
+       data := []byte("any + old & data")
+       str := base64.StdEncoding.EncodeToString(data)
+       fmt.Println(str)
+       // Output:
+       // YW55ICsgb2xkICYgZGF0YQ==
+}
+
+func ExampleEncoding_DecodeString() {
+       str := "c29tZSBkYXRhIHdpdGggACBhbmQg77u/"
+       data, err := base64.StdEncoding.DecodeString(str)
+       if err != nil {
+               fmt.Println("error:", err)
+               return
+       }
+       fmt.Printf("%q\n", data)
+       // Output:
+       // "some data with \x00 and \ufeff"
+}