yfhk / fabric_log

logs
1 stars 0 forks source link

base64 #25

Open yfhk opened 5 years ago

yfhk commented 5 years ago

package main

import ( "encoding/base64" "fmt" "log" )

func main() { input := []byte("hello golang base64 快乐编程http://www.01happy.com +~")

// 演示base64编码
encodeString := base64.StdEncoding.EncodeToString(input)
fmt.Println(encodeString)

// 对上面的编码结果进行base64解码
decodeBytes, err := base64.StdEncoding.DecodeString(encodeString)
if err != nil {
    log.Fatalln(err)
}
fmt.Println(string(decodeBytes))

fmt.Println()

// 如果要用在url中,需要使用URLEncoding
uEnc := base64.URLEncoding.EncodeToString([]byte(input))
fmt.Println(uEnc)

uDec, err := base64.URLEncoding.DecodeString(uEnc)
if err != nil {
    log.Fatalln(err)
}
fmt.Println(string(uDec))

}