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))
}

输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/go/go-base64-encoding.html

来源:LearnFk无涯教程网

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

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

技术教程推荐

从0开始学架构 -〔李运华〕

Elasticsearch核心技术与实战 -〔阮一鸣〕

研发效率破局之道 -〔葛俊〕

人人都能学会的编程入门课 -〔胡光〕

Service Mesh实战 -〔马若飞〕

人人都用得上的写作课 -〔涵柏〕

乔新亮的CTO成长复盘 -〔乔新亮〕

深入C语言和程序运行原理 -〔于航〕

结构思考力 · 透过结构看表达 -〔李忠秋〕

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