Golang教程-Go Base64编码

Go Base64编码
在Go中,我们可以对字符串和URL进行Base64编码。Go提供了编码器,它接受字节数组并将其转换为字符串编码。
解码器接受编码值并将其转换回原始字符串。
Go Base64示例
package main
import "fmt"
import b64 "encoding/base64"
func main() {
data := "JavaTpoint@12345!@#$%^&*()"
strEncode :=b64.StdEncoding.EncodeToString([]byte(data))
fmt.Println("要进行编码的值:"+data)
fmt.Println("编码后的值:"+strEncode)
fmt.Println()
fmt.Print("要进行解码的值:"+strEncode)
strDecode, _ := b64.StdEncoding.DecodeString(strEncode)
fmt.Println("解码后的值:"+string( strDecode))
fmt.Println()
url := "https://golang.org/ref/spec"
fmt.Println("要进行编码的URL:"+url)
urlEncode := b64.URLEncoding.EncodeToString([]byte(url))
fmt.Println("编码后的URL:"+urlEncode)
fmt.Println("要进行解码的值:"+urlEncode)
strDecode2,_ := b64.URLEncoding.DecodeString(urlEncode)
fmt.Println("解码后的值:"+string(strDecode2))
}
输出:
要进行编码的值:JavaTpoint@12345!@#$%^&*()
编码后的值:SmF2YVRwb2ludEAxMjM0NSFAIyQlXiYqKCk=
要进行解码的值:SmF2YVRwb2ludEAxMjM0NSFAIyQlXiYqKCk=解码后的值:Ja-vaTpoint@12345!@#$%^&*()
要进行编码的URL:https://golang.org/ref/spec
编码后的URL:aHR0cHM6Ly9nb2xhbmcub3JnL3JlZi9zcGVj
要进行解码的值:aHR0cHM6Ly9nb2xhbmcub3JnL3JlZi9zcGVj
解码后的值:https://golang.org/ref/spec
进程在退出时的退出码为0