Go Base64
Go Base64编码
我们可以在 Go 中对 String 和 url 进行编码。 Go 有 Encoder,它接受字节数组并转换为字符串编码。
Decoder 接受编码值并将其转换为原始字符串。
Go Base64 示例
package main
import "fmt"
import b64 "encoding/base64"
func main() {
data := "lidihuo@12345!@#$%^&*()"
strEncode :=b64.StdEncoding.EncodeToString([]byte(data))
fmt.Println("value to be encode "+data)
fmt.Println("Encoden value: "+strEncode)
fmt.Println()
fmt.Print("Value to be decode "+strEncode)
strDecode, _ := b64.StdEncoding.DecodeString(strEncode)
fmt.Println("Decoded value "+string( strDecode))
fmt.Println()
url := "https://golang.org/ref/spec"
fmt.Println("url to be encode "+url)
urlEncode := b64.URLEncoding.EncodeToString([]byte(url))
fmt.Println("Encoded url "+urlEncode)
fmt.Println("value to be decode "+urlEncode)
strDecode2,_ := b64.URLEncoding.DecodeString(urlEncode)
fmt.Println("Decoded value "+string(strDecode2))
}
输出:
value to be encode "/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ad0fbecfbceeaf5f3f4eedaaba8a9aeaf">[email protected]"123352">[email protected]#$%^&*()
Encoden value: SmF2YVRwb2ludEAxMjM0NSFAIyQlXiYqKCk=
Value to be decode SmF2YVRwb2ludEAxMjM0NSFAIyQlXiYqKCk=Decoded value "e7ad86ca9186b397888e8993a7d6d5d4d3d2">[email protected]"2f0e6f">[email protected]#$%^&*()
url to be encode https://golang.org/ref/spec
Encoded url aHR0cHM6Ly9nb2xhbmcub3JnL3JlZi9zcGVj
value to be decode aHR0cHM6Ly9nb2xhbmcub3JnL3JlZi9zcGVj
Decoded value https://golang.org/ref/spec
Process finished with exit code 0