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讲 -〔周甲徳〕

大规模数据处理实战 -〔蔡元楠〕

正则表达式入门课 -〔涂伟忠〕

实用密码学 -〔范学雷〕

陶辉的网络协议集训班02期 -〔陶辉〕

讲好故事 -〔涵柏〕

Go进阶 · 分布式爬虫实战 -〔郑建勋〕

Vue 3 企业级项目实战课 -〔杨文坚〕

徐昊 · AI 时代的软件工程 -〔徐昊〕

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