Encode/Decode base64

前端 未结 5 1019
陌清茗
陌清茗 2021-02-06 22:06

here is my code and i don\'t understand why the decode function doesn\'t work.

Little insight would be great please.

func EncodeB64(message string) (reto         


        
5条回答
  •  南笙
    南笙 (楼主)
    2021-02-06 22:54

    To sum up the other two posts, here are two simple functions to encode/decode Base64 strings with Go:

    // Dont forget to import "encoding/base64"!
    
    func base64Encode(str string) string {
        return base64.StdEncoding.EncodeToString([]byte(str))
    }
    
    func base64Decode(str string) (string, bool) {
        data, err := base64.StdEncoding.DecodeString(str)
        if err != nil {
            return "", true
        }
        return string(data), false
    }
    

    Try it!

提交回复
热议问题