http://www.linuxidc.com/Linux/2014-06/103843.htm
package main
import (
"encoding/base64"
"fmt"
)
const (
base64Table = "1230QRSTUabcdVWXYZHijKLAWDCABDstEFGuvwxyzGHIJklmnopqr2345601789120"
)
var coder = base64.NewEncoding(base64Table)
func base64Encode(src []byte) []byte {
return []byte(coder.EncodeToString(src))
}
func base64Decode(src []byte) ([]byte, error) {
return coder.DecodeString(string(src))
}
func main() {
// encode
hello := "hello world"
debyte := base64Encode([]byte(hello))
// decode
fmt.Println(string(debyte))
enbyte, err := base64Decode(debyte)
if err != nil {
fmt.Println(err.Error())
}
if hello != string(enbyte) {
fmt.Println("hello is not equal to enbyte")
}
fmt.Println(string(enbyte))
}
有疑问加站长微信联系(非本文作者)