golang中base64的编码和解码可以用内置库encoding/base64 ~~~ package main import ( "encoding/base64" "fmt" "log" ) func main() { str := "hello world , hello golang !" fmt.Printf("string : %v\n", str) input := []byte(str) fmt.Printf("[]byte : %v\n", input) // 演示base64编码 encodeString := base64.StdEncoding.EncodeToString(input) fmt.Printf("encode base64 : %v\n", encodeString) // 对上面的编码结果进行base64解码 decodeBytes, err := base64.StdEncoding.DecodeString(encodeString) if err != nil { log.Fatalln(err) } fmt.Printf("decode base64 : %v\n", string(decodeBytes)) fmt.Println() // 如果要用在url中,需要使用URLEncoding urlencode := base64.URLEncoding.EncodeToString([]byte(input)) fmt.Printf("urlencode : %v\n", urlencode) //URLEncoding urldecode, err := base64.URLEncoding.DecodeString(urlencode) if err != nil { log.Fatalln(err) } fmt.Printf("urldecode : %v\n", string(urldecode)) } ~~~ 输出结果: ~~~ string : hello world , hello golang ! []byte : [104 101 108 108 111 32 119 111 114 108 100 32 44 32 104 101 108 108 111 32 103 111 108 97 110 103 32 33] encode base64 : aGVsbG8gd29ybGQgLCBoZWxsbyBnb2xhbmcgIQ== decode base64 : hello world , hello golang ! urlencode : aGVsbG8gd29ybGQgLCBoZWxsbyBnb2xhbmcgIQ== urldecode : hello world , hello golang ! ~~~