# 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==
```
- 库
- package achive
- package tar
- package zip
- package bufio
- package builtin
- package bytes
- package compress
- package bzip2
- package flate
- package gzip
- package lzw
- package zlib
- package container
- package heap
- package list
- package ring
- package crypto
- package aes
- package cipher
- package des
- package dsa
- package ecdsa
- package elliptic
- package hmac
- package md5
- package rand
- package rc4
- package rsa
- package sha1
- package sha256
- package sha512
- package subtle
- package tls
- package x509
- package pkix
- package database
- package sql
- package driver
- package encoding
- package ascii85
- package asn1
- package base32
- package base64
- package binary
- package csv
- package gob
- package hex
- package json
- package pem
- package xml
- package errors
- package expvar
- package flag
- package fmt
- package go
- package doc
- package format
- package parser
- package printer
- package hash
- package adler32
- package crc32
- package crc64
- package fnv
- package html
- package template
- package image
- package color
- package palette
- package draw
- package gif
- package jpeg
- package png
- package index
- package suffixarray
- package io
- package ioutil
- package log
- package syslog
- package math
- package big
- package cmplx
- package rand
- package mime
- package multipart
- package net
- package http
- package cgi
- package cookiejar
- package fcgi
- package httptest
- package httputil
- package pprof
- package mail
- package rpc
- package jsonrpc
- package smtp
- package textproto
- package url
- package os
- package exec
- package signal
- package user
- package path
- package filepath
- package reflect
- package regexp
- package runtime
- package cgo
- package debug
- package pprof
- package race
- package sort
- package strconv
- package strings
- package sync
- package atomic
- package text
- package scanner
- package tabwriter
- package template
- package time
- package unicode
- package utf16
- package utf8
- package unsafe