ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# package base64 `import "encoding/base64"` base64实现了[RFC 4648](http://tools.ietf.org/html/rfc4648)规定的base64编码。 ## Index * [Variables](#pkg-variables) * [type CorruptInputError](#CorruptInputError) * [func (e CorruptInputError) Error() string](#CorruptInputError.Error) * [type Encoding](#Encoding) * [func NewEncoding(encoder string) \*Encoding](#NewEncoding) * [func (enc \*Encoding) DecodedLen(n int) int](#Encoding.DecodedLen) * [func (enc \*Encoding) Decode(dst, src []byte) (n int, err error)](#Encoding.Decode) * [func (enc \*Encoding) DecodeString(s string) ([]byte, error)](#Encoding.DecodeString) * [func (enc \*Encoding) EncodedLen(n int) int](#Encoding.EncodedLen) * [func (enc \*Encoding) Encode(dst, src []byte)](#Encoding.Encode) * [func (enc \*Encoding) EncodeToString(src []byte) string](#Encoding.EncodeToString) * [func NewDecoder(enc \*Encoding, r io.Reader) io.Reader](#NewDecoder) * [func NewEncoder(enc \*Encoding, w io.Writer) io.WriteCloser](#NewEncoder) ### Examples * [Encoding.DecodeString](#example-Encoding-DecodeString) * [Encoding.EncodeToString](#example-Encoding-EncodeToString) * [NewEncoder](#example-NewEncoder) ## Variables ``` var StdEncoding = NewEncoding(encodeStd) ``` RFC 4648定义的标准base64编码字符集。 ``` var URLEncoding = NewEncoding(encodeURL) ``` RFC 4648定义的另一base64编码字符集,用于URL和文件名。 ## type [CorruptInputError](https://github.com/golang/go/blob/master/src/encoding/base64/base64.go#L210 "View Source") ``` type CorruptInputError int64 ``` ### func (CorruptInputError) [Error](https://github.com/golang/go/blob/master/src/encoding/base64/base64.go#L212 "View Source") ``` func (e CorruptInputError) Error() string ``` ## type [Encoding](https://github.com/golang/go/blob/master/src/encoding/base64/base64.go#L24 "View Source") ``` type Encoding struct { // 内含隐藏或非导出字段 } ``` 双向的编码/解码协议,根据一个64字符的字符集定义,[RFC 4648](http://tools.ietf.org/html/rfc4648)标准化了两种字符集。默认字符集用于MIME([RFC 2045](http://tools.ietf.org/html/rfc2045))和PEM([RFC 1421](http://tools.ietf.org/html/rfc1421))编码;另一种用于URL和文件名,用'-'和'\_'替换了'+'和'/'。 ### func [NewEncoding](https://github.com/golang/go/blob/master/src/encoding/base64/base64.go#L34 "View Source") ``` func NewEncoding(encoder string) *Encoding ``` 使用给出的字符集生成一个\*Encoding,字符集必须是64字节的字符串。 ### func (\*Encoding) [DecodedLen](https://github.com/golang/go/blob/master/src/encoding/base64/base64.go#L393 "View Source") ``` func (enc *Encoding) DecodedLen(n int) int ``` 返回n字节base64编码的数据解码后的最大长度。 ### func (\*Encoding) [Decode](https://github.com/golang/go/blob/master/src/encoding/base64/base64.go#L288 "View Source") ``` func (enc *Encoding) Decode(dst, src []byte) (n int, err error) ``` 将src的数据解码后存入dst,最多写DecodedLen(len(src))字节数据到dst,并返回写入的字节数。 如果src包含非法字符,将返回成功写入的字符数和CorruptInputError。换行符(\r、\n)会被忽略。 ### func (\*Encoding) [DecodeString](https://github.com/golang/go/blob/master/src/encoding/base64/base64.go#L295 "View Source") ``` func (enc *Encoding) DecodeString(s string) ([]byte, error) ``` 返回base64编码的字符串s代表的数据。 Example ``` 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" ``` ### func (\*Encoding) [EncodedLen](https://github.com/golang/go/blob/master/src/encoding/base64/base64.go#L204 "View Source") ``` func (enc *Encoding) EncodedLen(n int) int ``` 返回n字节数据进行base64编码后的最大长度。 ### func (\*Encoding) [Encode](https://github.com/golang/go/blob/master/src/encoding/base64/base64.go#L71 "View Source") ``` func (enc *Encoding) Encode(dst, src []byte) ``` 将src的数据编码后存入dst,最多写EncodedLen(len(src))字节数据到dst,并返回写入的字节数。  函数会把输出设置为4的倍数,因此不建议对大数据流的独立数据块执行此方法,使用NewEncoder()代替。 ### func (\*Encoding) [EncodeToString](https://github.com/golang/go/blob/master/src/encoding/base64/base64.go#L118 "View Source") ``` func (enc *Encoding) EncodeToString(src []byte) string ``` 返回将src编码后的字符串。 Example ``` data := []byte("any + old & data") str := base64.StdEncoding.EncodeToString(data) fmt.Println(str) ``` Output: ``` YW55ICsgb2xkICYgZGF0YQ== ``` ## func [NewDecoder](https://github.com/golang/go/blob/master/src/encoding/base64/base64.go#L387 "View Source") ``` func NewDecoder(enc *Encoding, r io.Reader) io.Reader ``` 创建一个新的base64流解码器。 ## func [NewEncoder](https://github.com/golang/go/blob/master/src/encoding/base64/base64.go#L198 "View Source") ``` func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser ``` 创建一个新的base64流编码器。写入的数据会在编码后再写入w,base32编码每3字节执行一次编码操作;写入完毕后,使用者必须调用Close方法以便将未写入的数据从缓存中刷新到w中。 Example ``` 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== ```