summaryrefslogtreecommitdiff
path: root/devdocs/go/encoding%2Fbase64%2Findex.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/go/encoding%2Fbase64%2Findex.html
new repository
Diffstat (limited to 'devdocs/go/encoding%2Fbase64%2Findex.html')
-rw-r--r--devdocs/go/encoding%2Fbase64%2Findex.html114
1 files changed, 114 insertions, 0 deletions
diff --git a/devdocs/go/encoding%2Fbase64%2Findex.html b/devdocs/go/encoding%2Fbase64%2Findex.html
new file mode 100644
index 00000000..91acad34
--- /dev/null
+++ b/devdocs/go/encoding%2Fbase64%2Findex.html
@@ -0,0 +1,114 @@
+<h1> Package base64 </h1> <ul id="short-nav">
+<li><code>import "encoding/base64"</code></li>
+<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
+<li><a href="#pkg-index" class="indexLink">Index</a></li>
+<li><a href="#pkg-examples" class="examplesLink">Examples</a></li>
+</ul> <h2 id="pkg-overview">Overview </h2> <p>Package base64 implements base64 encoding as specified by RFC 4648. </p> <h4 id="example_"> <span class="text">Example</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">msg := "Hello, 世界"
+encoded := base64.StdEncoding.EncodeToString([]byte(msg))
+fmt.Println(encoded)
+decoded, err := base64.StdEncoding.DecodeString(encoded)
+if err != nil {
+ fmt.Println("decode error:", err)
+ return
+}
+fmt.Println(string(decoded))
+</pre> <p>Output:</p> <pre class="output" data-language="go">SGVsbG8sIOS4lueVjA==
+Hello, 世界
+</pre> <h2 id="pkg-index">Index </h2> <ul id="manual-nav">
+<li><a href="#pkg-constants">Constants</a></li>
+<li><a href="#pkg-variables">Variables</a></li>
+<li><a href="#NewDecoder">func NewDecoder(enc *Encoding, r io.Reader) io.Reader</a></li>
+<li><a href="#NewEncoder">func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser</a></li>
+<li><a href="#CorruptInputError">type CorruptInputError</a></li>
+<li> <a href="#CorruptInputError.Error">func (e CorruptInputError) Error() string</a>
+</li>
+<li><a href="#Encoding">type Encoding</a></li>
+<li> <a href="#NewEncoding">func NewEncoding(encoder string) *Encoding</a>
+</li>
+<li> <a href="#Encoding.AppendDecode">func (enc *Encoding) AppendDecode(dst, src []byte) ([]byte, error)</a>
+</li>
+<li> <a href="#Encoding.AppendEncode">func (enc *Encoding) AppendEncode(dst, src []byte) []byte</a>
+</li>
+<li> <a href="#Encoding.Decode">func (enc *Encoding) Decode(dst, src []byte) (n int, err error)</a>
+</li>
+<li> <a href="#Encoding.DecodeString">func (enc *Encoding) DecodeString(s string) ([]byte, error)</a>
+</li>
+<li> <a href="#Encoding.DecodedLen">func (enc *Encoding) DecodedLen(n int) int</a>
+</li>
+<li> <a href="#Encoding.Encode">func (enc *Encoding) Encode(dst, src []byte)</a>
+</li>
+<li> <a href="#Encoding.EncodeToString">func (enc *Encoding) EncodeToString(src []byte) string</a>
+</li>
+<li> <a href="#Encoding.EncodedLen">func (enc *Encoding) EncodedLen(n int) int</a>
+</li>
+<li> <a href="#Encoding.Strict">func (enc Encoding) Strict() *Encoding</a>
+</li>
+<li> <a href="#Encoding.WithPadding">func (enc Encoding) WithPadding(padding rune) *Encoding</a>
+</li>
+</ul> <div id="pkg-examples"> <h3>Examples</h3> <dl> <dd><a class="exampleLink" href="#example_">Package</a></dd> <dd><a class="exampleLink" href="#example_Encoding_Decode">Encoding.Decode</a></dd> <dd><a class="exampleLink" href="#example_Encoding_DecodeString">Encoding.DecodeString</a></dd> <dd><a class="exampleLink" href="#example_Encoding_Encode">Encoding.Encode</a></dd> <dd><a class="exampleLink" href="#example_Encoding_EncodeToString">Encoding.EncodeToString</a></dd> <dd><a class="exampleLink" href="#example_NewEncoder">NewEncoder</a></dd> </dl> </div> <h3>Package files</h3> <p> <span>base64.go</span> </p> <h2 id="pkg-constants">Constants</h2> <pre data-language="go">const (
+ StdPadding rune = '=' // Standard padding character
+ NoPadding rune = -1 // No padding
+)</pre> <h2 id="pkg-variables">Variables</h2> <p>RawStdEncoding is the standard raw, unpadded base64 encoding, as defined in RFC 4648 section 3.2. This is the same as <a href="#StdEncoding">StdEncoding</a> but omits padding characters. </p>
+<pre data-language="go">var RawStdEncoding = StdEncoding.WithPadding(NoPadding)</pre> <p>RawURLEncoding is the unpadded alternate base64 encoding defined in RFC 4648. It is typically used in URLs and file names. This is the same as <a href="#URLEncoding">URLEncoding</a> but omits padding characters. </p>
+<pre data-language="go">var RawURLEncoding = URLEncoding.WithPadding(NoPadding)</pre> <p>StdEncoding is the standard base64 encoding, as defined in RFC 4648. </p>
+<pre data-language="go">var StdEncoding = NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")</pre> <p>URLEncoding is the alternate base64 encoding defined in RFC 4648. It is typically used in URLs and file names. </p>
+<pre data-language="go">var URLEncoding = NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")</pre> <h2 id="NewDecoder">func <span>NewDecoder</span> </h2> <pre data-language="go">func NewDecoder(enc *Encoding, r io.Reader) io.Reader</pre> <p>NewDecoder constructs a new base64 stream decoder. </p>
+<h2 id="NewEncoder">func <span>NewEncoder</span> </h2> <pre data-language="go">func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser</pre> <p>NewEncoder returns a new base64 stream encoder. Data written to the returned writer will be encoded using enc and then written to w. Base64 encodings operate in 4-byte blocks; when finished writing, the caller must Close the returned encoder to flush any partially written blocks. </p> <h4 id="example_NewEncoder"> <span class="text">Example</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">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()
+</pre> <p>Output:</p> <pre class="output" data-language="go">Zm9vAGJhcg==
+</pre> <h2 id="CorruptInputError">type <span>CorruptInputError</span> </h2> <pre data-language="go">type CorruptInputError int64</pre> <h3 id="CorruptInputError.Error">func (CorruptInputError) <span>Error</span> </h3> <pre data-language="go">func (e CorruptInputError) Error() string</pre> <h2 id="Encoding">type <span>Encoding</span> </h2> <p>An Encoding is a radix 64 encoding/decoding scheme, defined by a 64-character alphabet. The most common encoding is the "base64" encoding defined in RFC 4648 and used in MIME (RFC 2045) and PEM (RFC 1421). RFC 4648 also defines an alternate encoding, which is the standard encoding with - and _ substituted for + and /. </p>
+<pre data-language="go">type Encoding struct {
+ // contains filtered or unexported fields
+}
+</pre> <h3 id="NewEncoding">func <span>NewEncoding</span> </h3> <pre data-language="go">func NewEncoding(encoder string) *Encoding</pre> <p>NewEncoding returns a new padded Encoding defined by the given alphabet, which must be a 64-byte string that contains unique byte values and does not contain the padding character or CR / LF ('\r', '\n'). The alphabet is treated as a sequence of byte values without any special treatment for multi-byte UTF-8. The resulting Encoding uses the default padding character ('='), which may be changed or disabled via <a href="#Encoding.WithPadding">Encoding.WithPadding</a>. </p>
+<h3 id="Encoding.AppendDecode">func (*Encoding) <span>AppendDecode</span> <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (enc *Encoding) AppendDecode(dst, src []byte) ([]byte, error)</pre> <p>AppendDecode appends the base64 decoded src to dst and returns the extended buffer. If the input is malformed, it returns the partially decoded src and an error. </p>
+<h3 id="Encoding.AppendEncode">func (*Encoding) <span>AppendEncode</span> <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (enc *Encoding) AppendEncode(dst, src []byte) []byte</pre> <p>AppendEncode appends the base64 encoded src to dst and returns the extended buffer. </p>
+<h3 id="Encoding.Decode">func (*Encoding) <span>Decode</span> </h3> <pre data-language="go">func (enc *Encoding) Decode(dst, src []byte) (n int, err error)</pre> <p>Decode decodes src using the encoding enc. It writes at most <a href="#Encoding.DecodedLen">Encoding.DecodedLen</a>(len(src)) bytes to dst and returns the number of bytes written. If src contains invalid base64 data, it will return the number of bytes successfully written and <a href="#CorruptInputError">CorruptInputError</a>. New line characters (\r and \n) are ignored. </p> <h4 id="example_Encoding_Decode"> <span class="text">Example</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">str := "SGVsbG8sIHdvcmxkIQ=="
+dst := make([]byte, base64.StdEncoding.DecodedLen(len(str)))
+n, err := base64.StdEncoding.Decode(dst, []byte(str))
+if err != nil {
+ fmt.Println("decode error:", err)
+ return
+}
+dst = dst[:n]
+fmt.Printf("%q\n", dst)
+</pre> <p>Output:</p> <pre class="output" data-language="go">"Hello, world!"
+</pre> <h3 id="Encoding.DecodeString">func (*Encoding) <span>DecodeString</span> </h3> <pre data-language="go">func (enc *Encoding) DecodeString(s string) ([]byte, error)</pre> <p>DecodeString returns the bytes represented by the base64 string s. </p> <h4 id="example_Encoding_DecodeString"> <span class="text">Example</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">str := "c29tZSBkYXRhIHdpdGggACBhbmQg77u/"
+data, err := base64.StdEncoding.DecodeString(str)
+if err != nil {
+ fmt.Println("error:", err)
+ return
+}
+fmt.Printf("%q\n", data)
+</pre> <p>Output:</p> <pre class="output" data-language="go">"some data with \x00 and \ufeff"
+</pre> <h3 id="Encoding.DecodedLen">func (*Encoding) <span>DecodedLen</span> </h3> <pre data-language="go">func (enc *Encoding) DecodedLen(n int) int</pre> <p>DecodedLen returns the maximum length in bytes of the decoded data corresponding to n bytes of base64-encoded data. </p>
+<h3 id="Encoding.Encode">func (*Encoding) <span>Encode</span> </h3> <pre data-language="go">func (enc *Encoding) Encode(dst, src []byte)</pre> <p>Encode encodes src using the encoding enc, writing <a href="#Encoding.EncodedLen">Encoding.EncodedLen</a>(len(src)) bytes to dst. </p>
+<p>The encoding pads the output to a multiple of 4 bytes, so Encode is not appropriate for use on individual blocks of a large data stream. Use <a href="#NewEncoder">NewEncoder</a> instead. </p> <h4 id="example_Encoding_Encode"> <span class="text">Example</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">data := []byte("Hello, world!")
+dst := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
+base64.StdEncoding.Encode(dst, data)
+fmt.Println(string(dst))
+</pre> <p>Output:</p> <pre class="output" data-language="go">SGVsbG8sIHdvcmxkIQ==
+</pre> <h3 id="Encoding.EncodeToString">func (*Encoding) <span>EncodeToString</span> </h3> <pre data-language="go">func (enc *Encoding) EncodeToString(src []byte) string</pre> <p>EncodeToString returns the base64 encoding of src. </p> <h4 id="example_Encoding_EncodeToString"> <span class="text">Example</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">data := []byte("any + old &amp; data")
+str := base64.StdEncoding.EncodeToString(data)
+fmt.Println(str)
+</pre> <p>Output:</p> <pre class="output" data-language="go">YW55ICsgb2xkICYgZGF0YQ==
+</pre> <h3 id="Encoding.EncodedLen">func (*Encoding) <span>EncodedLen</span> </h3> <pre data-language="go">func (enc *Encoding) EncodedLen(n int) int</pre> <p>EncodedLen returns the length in bytes of the base64 encoding of an input buffer of length n. </p>
+<h3 id="Encoding.Strict">func (Encoding) <span>Strict</span> <span title="Added in Go 1.8">1.8</span> </h3> <pre data-language="go">func (enc Encoding) Strict() *Encoding</pre> <p>Strict creates a new encoding identical to enc except with strict decoding enabled. In this mode, the decoder requires that trailing padding bits are zero, as described in RFC 4648 section 3.5. </p>
+<p>Note that the input is still malleable, as new line characters (CR and LF) are still ignored. </p>
+<h3 id="Encoding.WithPadding">func (Encoding) <span>WithPadding</span> <span title="Added in Go 1.5">1.5</span> </h3> <pre data-language="go">func (enc Encoding) WithPadding(padding rune) *Encoding</pre> <p>WithPadding creates a new encoding identical to enc except with a specified padding character, or <a href="#NoPadding">NoPadding</a> to disable padding. The padding character must not be '\r' or '\n', must not be contained in the encoding's alphabet, must not be negative, and must be a rune equal or below '\xff'. Padding characters above '\x7f' are encoded as their exact byte value rather than using the UTF-8 representation of the codepoint. </p><div class="_attribution">
+ <p class="_attribution-p">
+ &copy; Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
+ <a href="http://golang.org/pkg/encoding/base64/" class="_attribution-link">http://golang.org/pkg/encoding/base64/</a>
+ </p>
+</div>