Go - Base64

Go - Base64 首页 / Golang入门教程 / Go - Base64

我们可以在Go中编码String和url。 Go有带字节数组并转换为字符串编码的Encoder。

解码器获取编码后的值,并将其转换为原始字符串。

无涯教程网

Go Base64示例

package main

import "fmt"
import b64 "encoding/base64"
func main() {
	data := "Learnfk@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  Learnfk@12345!@#$%^&*()
Encoden value:  SmF2YVRwb2ludEAxMjM0NSFAIyQlXiYqKCk=

Value to be decode  SmF2YVRwb2ludEAxMjM0NSFAIyQlXiYqKCk=Decoded value  Ja-vaTpoint@12345!@#$%^&*()

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

白话法律42讲 -〔周甲徳〕

Linux性能优化实战 -〔倪朋飞〕

Web安全攻防实战 -〔王昊天〕

如何看懂一幅画 -〔罗桂霞〕

Python自动化办公实战课 -〔尹会生〕

中间件核心技术与实战 -〔丁威〕

超级访谈:对话毕玄 -〔毕玄〕

手把手教你落地DDD -〔钟敬〕

超级访谈:对话道哥 -〔吴翰清(道哥)〕

好记忆不如烂笔头。留下您的足迹吧 :)