file与base64互转
package main
import (
"encoding/base64"
"fmt"
"io/ioutil"
)
func main() {
// file 转 base64
fileBytes, err := ioutil.ReadFile("E:\\测试\\test.png") // 读取file
if err != nil {
panic(err)
}
bs64 := base64.StdEncoding.EncodeToString(fileBytes) // 加密成base64字符串
fmt.Println(bs64)
// base64 转 file
bs64Bytes, err := base64.StdEncoding.DecodeString(bs64) // 解密base64字符串
if err != nil {
panic(err)
}
err = ioutil.WriteFile("tmp/base64tofile/test.jpg", bs64Bytes, 0666) // 写入file
if err != nil {
panic(err)
}
}
标签:ioutil,文件,err,nil,base64,file,go,panic
From: https://www.cnblogs.com/wangjinkui/p/17306195.html