📜  Golang 中的 base64 包

📅  最后修改于: 2021-10-25 02:07:40             🧑  作者: Mango

Go 语言为 base64 编码/解码提供内置支持,并具有可用于使用 base64 包对给定数据执行操作的函数。

Function Description
NewDecoder This function is used to construct a new base64 stream decoder.
NewEncoder This function is used to return a new base64 stream encoder.

类型编码:编码是由 64 个字符的字母表定义的基数 64 编码/解码方案。最常见的编码是 RFC 4648 中定义的“base64”编码,它用于 MIME (RFC 2045) 和 PEM (RFC 1421)。 RFC 4648 还定义了一种替代编码,它是标准编码,用 – 和 _ 代替 + 和 /。

type Encoding struct {
    // It holds filtered or unexported fields
}
Method Description
func NewEncoding This function is used to return a new padded Encoding defined by the given alphabet, which must be a 64-byte string that does not contain the padding character or CR / LF (‘\r’, ‘\n’).
func(*Encoding) Decode This method is used to decodes src using the encoding end. This method will writes at most DecodedLen(len(src)) bytes to dst and returns the number of bytes written. If src contains invalid base64 data, then this method will return the number of bytes successfully written and CorruptInputError. Here, new line characters (\r and \n) are ignored.
func (*Encoding) DecodeString This method is used to return the bytes represented by the base64 string s.
func (*Encoding) DecodedLen This method is used to return the maximum length in bytes of the decoded data corresponding to n bytes of base64-encoded data.
func (*Encoding) Encode This method is used to encodes src using the encoding enc, writing EncodedLen(len(src)) bytes to dst.
func (*Encoding) EncodeToString This method is used to return the base64 encoding of src.
func (*Encoding) EncodedLen This method is used to return the length in bytes of the base64 encoding of an input buffer of length n.
func (Encoding) Strict This method is used to create a new encoding identical to enc except with strict decoding enabled.
func (Encoding) WithPadding This method is used to create a new encoding identical to enc except with a specified padding character, or NoPadding to disable padding.

示例 1:

// Golang program to illustrate
// the base64.DecodeString() Function
package main
  
import (
    "encoding/base64"
    "fmt"
)
  
func main() {
  
    // Taking a string
    givenString := "R2Vla3Nmb3JHZWVrcw=="
  
    // using the function
    decodedString, err := base64.StdEncoding.DecodeString(givenString)
    if err != nil {
        fmt.Println("Error Found:", err)
        return
    }
  
    fmt.Print("Decoded Bytes: ")
    fmt.Println(decodedString)
  
    fmt.Print("Decoded String: ")
    fmt.Println(string(decodedString))
}

输出:

Decoded Bytes: [71 101 101 107 115 102 111 114 71 101 101 107 115]
Decoded String: GeeksforGeeks

示例 2:

// Golang program to illustrate
// the base64.NewEncoder() function
package main
  
import (
    "encoding/base64"
    "fmt"
)
  
func main() {
  
    // Input string
    giveninput := []byte("GeeksforGeeks")
  
    // Using EncodeToString() function
    // Encode the given string
    result := base64.StdEncoding.EncodeToString(giveninput)
    fmt.Println("Encoded string: ", result)
}

输出:

Encoded string:  R2Vla3Nmb3JHZWVrcw==