Base64 Encoding {.en}

Base64编码 {.zh}

::: {.en} Go provides built-in support for base64 encoding/decoding. :::

::: {.zh}

Go为[base64encoding / decoding](http://en.wikipedia.org/wiki/Base64)提供内置支持。

:::

  1. package main

::: {.en} This syntax imports the encoding/base64 package with the b64 name instead of the default base64. It’ll save us some space below. :::

::: {.zh}

此语法使用b64名称而不是默认的base64导入encoding / base64包。它会给我们留下一些空间。

:::

  1. import b64 "encoding/base64"
  2. import "fmt"
  3. func main() {

::: {.en} Here’s the string we’ll encode/decode. :::

::: {.zh}

这是我们将编码/解码的`字符串’。

:::

  1. data := "abc123!?$*&()'-=@~"

::: {.en} Go supports both standard and URL-compatible base64. Here’s how to encode using the standard encoder. The encoder requires a []byte so we convert our string to that type. :::

::: {.zh}

Go支持标准和URL兼容的base64。以下是使用standardencoder进行编码的方法。编码器需要一个[] byte,所以我们将string转换为该类型。

:::

  1. sEnc := b64.StdEncoding.EncodeToString([]byte(data))
  2. fmt.Println(sEnc)

::: {.en} Decoding may return an error, which you can check if you don’t already know the input to be well-formed. :::

::: {.zh}

解码可能会返回错误,您可以检查是否您还不知道输入是否格式正确。

:::

  1. sDec, _ := b64.StdEncoding.DecodeString(sEnc)
  2. fmt.Println(string(sDec))
  3. fmt.Println()

::: {.en} This encodes/decodes using a URL-compatible base64 format. :::

::: {.zh}

这使用与URL兼容的base64format进行编码/解码。

:::

  1. uEnc := b64.URLEncoding.EncodeToString([]byte(data))
  2. fmt.Println(uEnc)
  3. uDec, _ := b64.URLEncoding.DecodeString(uEnc)
  4. fmt.Println(string(uDec))
  5. }

::: {.en} The string encodes to slightly different values with the standard and URL base64 encoders (trailing + vs -) but they both decode to the original string as desired. :::

::: {.zh}

该字符串使用标准和URL base64编码器(尾随+vs-)编码略有不同的值,但它们都根据需要解码为原始字符串。

:::

  1. $ go run base64-encoding.go
  2. YWJjMTIzIT8kKiYoKSctPUB+
  3. abc123!?$*&()'-=@~
  4. YWJjMTIzIT8kKiYoKSctPUB-
  5. abc123!?$*&()'-=@~