Base64 Decode in Golang

  • 15 April 2020
  • ADM

 

Base64 Decode in Golang - images/logos/golang.jpg

 

Golang programing language provides built-in suport for base64 decoding. The base64 functions are available into the encoding/base64 package. It provides implementations for both Standard, with or without padding and Filename safe Base64 decoding variant.

Note: if you need an online tool to decode an base64 to string, you can use Base64 Decoder Online tool, also available for files.

 

Golang Base64 decoding

package main

import (
	"encoding/base64"
	"fmt"
)

func main() {

	base64Str := "VGhpcyBpcyBhIHRlc3QhIQ=="

	str, err := base64.StdEncoding.DecodeString(base64Str)
	if err != nil {
		fmt.Println("ERROR: ", err)
		return
	}

	fmt.Println("Base64: ", base64Str)
	fmt.Println("String: ", string(str))
}

Output

Base64:  VGhpcyBpcyBhIHRlc3QhIQ==
String:  This is a test!!

To check the encode examples, please check the Base64 Encode Golang tutorial.

Errors

If the base64 string is not correct you would get an error like this:

ERROR:  illegal base64 data at input byte 22

 

Golang Base64 URL Decoding

package main

import (
	"encoding/base64"
	"fmt"
)

func main() {

	base64Str := "VGhpcyBpcyBhIHRlc3QgZm9yICsgVVJMICsgZW5jb2RpbmcgXCBiYXNlNjQgISA_IC4="

	str, err := base64.URLEncoding.DecodeString(base64Str)
	if err != nil {
		fmt.Println("ERROR: ", err)
		return
	}

	fmt.Println("Base64: ", base64Str)
	fmt.Println("String: ", string(str))
}

Output

Base64:  VGhpcyBpcyBhIHRlc3QgZm9yICsgVVJMICsgZW5jb2RpbmcgXCBiYXNlNjQgISA_IC4=
String:  This is a test for + URL + encoding  base64 ! ? .

 

Golang Base64 Decoding without Padding

To decode without the padding, you can use the Raw decodings provided by the encoding/base64 Golang package.

The Raw decodings are for both Standard and URL encoding.

package main

import (
	"encoding/base64"
	"fmt"
)

func main() {

	base64Str := "VGhpcyBpcyBhIHRlc3QgZm9yICsgVVJMICsgZW5jb2RpbmcgXCBiYXNlNjQgISA/IC4"
	base64URL := "VGhpcyBpcyBhIHRlc3QgZm9yICsgVVJMICsgZW5jb2RpbmcgXCBiYXNlNjQgISA_IC4"

	str, err := base64.RawStdEncoding.DecodeString(base64Str)
	if err != nil {
		fmt.Println("ERROR: ", err)
		return
	}
	url, err := base64.RawURLEncoding.DecodeString(base64URL)
	if err != nil {
		fmt.Println("ERROR: ", err)
		return
	}

	fmt.Println("Base64 Standard, no padding: ", base64Str)
	fmt.Println("Base64 URL, no padding: ", base64URL)
	fmt.Println("Standard String: ", string(str))
	fmt.Println("URL String: ", string(url))
}

Output

Base64 Standard, no padding:  VGhpcyBpcyBhIHRlc3QgZm9yICsgVVJMICsgZW5jb2RpbmcgXCBiYXNlNjQgISA/IC4
Base64 URL, no padding:  VGhpcyBpcyBhIHRlc3QgZm9yICsgVVJMICsgZW5jb2RpbmcgXCBiYXNlNjQgISA_IC4
Standard String:  This is a test for + URL + encoding  base64 ! ? .
URL String:  This is a test for + URL + encoding  base64 ! ? .

 

Compile&Run

To compile the code navigate to the file location and run the following command.

$ go build example.go

Assuming that example.go is the name of your file.

Then depending on if you are on Linux or Windows the binary file is created.

To run the application, execute the command.

Linux

$ ./example

Windows

c:\Users\adm\go\tutorials> example.exe

If you want to compile and run the application in one single step, run the following command:

go run example.go

 

References