golang decode base64 code example

Example 1: base64 encode golang

sEnc := base64.StdEncoding.EncodeToString([]byte(data))
    fmt.Println(sEnc)

Example 2: golang decode base64 file

package main

import (
	"fmt"
    "encoding/base64"
)

var b64 = `TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg
dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu
dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo
ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=`

func main() {
    dec, err := base64.StdEncoding.DecodeString(b64)
    if err != nil {
        panic(err)
    }
    
    //print content or store it into a local file system
    fmt.Println(string(des))

}

Tags:

Go Example