go语言中使用Base64
Base64编码,是我们程序开发中经常使?到的编码?法。
因为base64编码的字符串,更适合不同平台、
不同语?的传输(?个字符可能其他的系统没有)。
它是?种基于?64个可打印字符来表示?进制
数据的表示?法。它通常?作存储、传输?些
?进制数据编码?法,
?句话:将?进制数据?本化(转成ASCII)。
func main() {
text := "你好 世界 hello world"
textBase64 := GenerateBase64(text)
fmt.Println(textBase64)
newText := DecodeBase64(textBase64)
fmt.Println(newText)
}
func DecodeBase64(textBase64 string) string {
bytes, err := base64.StdEncoding.DecodeString(textBase64)
if err != nil {
panic(err)
}
return string(bytes)
}
func GenerateBase64(text string) string {
textBytes := []byte(text)
textBase64 := base64.StdEncoding.EncodeToString(textBytes)
return textBase64
}