]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/base64: new example for NewEncoder with emphasis on Close
authorShenghou Ma <minux.ma@gmail.com>
Tue, 11 Dec 2012 16:49:02 +0000 (11:49 -0500)
committerRuss Cox <rsc@golang.org>
Tue, 11 Dec 2012 16:49:02 +0000 (11:49 -0500)
Fixes #4394.

R=iant, bradfitz, rsc, remigius.gieben
CC=golang-dev
https://golang.org/cl/6847125

src/pkg/encoding/base32/example_test.go
src/pkg/encoding/base64/example_test.go

index bd16ca1c794a053dc7a520c1f8a325b901334ef0..f6128d90035066fc66adbbe0fb757dc6a46ce6f3 100644 (file)
@@ -2,11 +2,14 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// Keep in sync with ../base64/example_test.go.
+
 package base32_test
 
 import (
        "encoding/base32"
        "fmt"
+       "os"
 )
 
 func ExampleEncoding_EncodeToString() {
@@ -28,3 +31,15 @@ func ExampleEncoding_DecodeString() {
        // Output:
        // "some data with \x00 and \ufeff"
 }
+
+func ExampleNewEncoder() {
+       input := []byte("foo\x00bar")
+       encoder := base32.NewEncoder(base32.StdEncoding, os.Stdout)
+       encoder.Write(input)
+       // Must close the encoder when finished to flush any partial blocks.
+       // If you comment out the following line, the last partial block "r"
+       // won't be encoded.
+       encoder.Close()
+       // Output:
+       // MZXW6ADCMFZA====
+}
index 40b5438771dac24d9d6dc8c19138231477c7d51c..d18b856a0faace90d09939e93b6fcca3b91f6a9e 100644 (file)
@@ -2,11 +2,14 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// Keep in sync with ../base32/example_test.go.
+
 package base64_test
 
 import (
        "encoding/base64"
        "fmt"
+       "os"
 )
 
 func ExampleEncoding_EncodeToString() {
@@ -28,3 +31,15 @@ func ExampleEncoding_DecodeString() {
        // Output:
        // "some data with \x00 and \ufeff"
 }
+
+func ExampleNewEncoder() {
+       input := []byte("foo\x00bar")
+       encoder := base64.NewEncoder(base64.StdEncoding, os.Stdout)
+       encoder.Write(input)
+       // Must close the encoder when finished to flush any partial blocks.
+       // If you comment out the following line, the last partial block "r"
+       // won't be encoded.
+       encoder.Close()
+       // Output:
+       // Zm9vAGJhcg==
+}