Learn how to use Base64 in programming languages: C#, C++, C, Java
, ,

Base64 in Golang: How to Encode & Decode in Go Programming Language

Base64 encoding and decoding are essential in many programming languages, including the Go programming language (Golang). In this detailed guide, we will look into Base64 encoding and decoding in the Go programming language.

What is Base64?

Base64 is a binary-to-text encoding system that is essential for converting digital data into a text-based communication format. It achieves this by transforming binary data into a sequence of printable ASCII characters, allowing for secure transmission across a variety of text-oriented protocols. This encoding method is widely used when binary data, such as images or files, must be represented using just readable characters.

Base64 divides binary data into six-bit groups and maps these groups to specific ASCII letters. These characters were carefully chosen to ensure compatibility and secure transmission. While Base64 encoding increases data size by about one-third when compared to its original binary form, its dependability and suitability for text-based settings make it a popular technique in a variety of applications, particularly web development.

Encoding includes dividing binary data into little bits and mapping them to a specified set of letters, such as ‘A’ to ‘Z’, ‘0’ to ‘9’, and ‘+’ and ‘/’. The padding character ‘=’ ensures that the amount of characters in the encoded string is a multiple of four, resulting in a consistent format.

While Base64 encoding does not provide encryption or security in and of itself, it is a required tool for tasks like sending binary data over URLs, embedding graphics within web pages, and attaching files to emails. It is a useful method in a wide range of computer applications due to its widespread support across programming languages and ease of use.

What is Go or Golang?

Go, sometimes known as Golang, is an open-source programming language that focuses on simplicity, efficiency, and readability. Go was first announced in 2009 by Google programmers Robert Griesemer, Rob Pike, and Ken Thompson and has since garnered popularity among developers due to its current features and performance qualities.

Go was created to overcome the difficulties that developers have when dealing with huge codebases and complicated systems. Its syntax is simple and short, with a focus on readability, allowing developers to write and maintain code more easily. Go also places a premium on efficiency, making it ideal for activities requiring high performance, such as network programming, web development, and system-level programming.

Go has found applications in a wide range of domains, from web development and cloud computing to systems programming and DevOps tooling. Its simplicity, performance, and well-designed concurrency model make it a valuable language for building scalable and efficient software solutions.

Does Go Support Base64?

Go’s standard library includes built-in support for Base64 encoding and decoding. The encoding/base64 package in Go provides methods that enable developers to perform Base64 operations quickly and easily. This package contains functions for both encoding binary data into Base64 and decoding Base64-encoded data back into binary form.

Base64 Encoding in Go

The native Base64 encoding and decoding functionality in Go makes it simple for developers to interact with binary data in a variety of circumstances. Whether you need to encode binary data for data transfer, save binary data as a string, or decode Base64-encoded data received from other sources, Go’s standard library has the tools you need.

The encoding/base64 package provides the EncodeToString function to encode binary data into a Base64-encoded string. This function takes a byte slice as input and returns the corresponding Base64-encoded string.

In the following sections of this article, we’ll look at practical examples of how to use the encoding/base64 package in Go to encode and decode data, allowing you to use the power of Base64 encoding in your Go programs.

Example of Base64 Encoding in Golang

Let’s look at a simple example of using the package to conduct Base64 encoding.

In this example, the variable text contains the string “B64Encode”. We use the base64.StdEncoding.EncodeToString function to encode the text string into a Base64-encoded string. The function takes a byte slice as input, so we convert the text string to a byte slice using []byte(text).

package main
import (
func main() {
	// Text data
	text := "B64Encode"
	// Encode the text to Base64
	encoded := base64.StdEncoding.EncodeToString([]byte(text))
	// Print the Base64-encoded string

Base64 Decoding in Go Programming Language

To decode a Base64-encoded string back into its original binary form, the encoding/base64 package includes the DecodeString function. This function accepts a Base64-encoded string as input and returns the decoded byte slice.

Example of Base64 Decoding in Golang

In this example, the variable encoded contains the Base64-encoded string “QjY0RW5jb2Rl”. We use the base64.StdEncoding.DecodeString function to decode the Base64-encoded string back to a byte slice. If the decoding is successful, we convert the byte slice to a text string using string(decodedBytes).

package main
import (
func main() {
	// Base64-encoded string
	encoded := "QjY0RW5jb2Rl"
	// Decode the Base64-encoded string
	decodedBytes, err := base64.StdEncoding.DecodeString(encoded)
	if err != nil {
		fmt.Println("Error decoding:", err)
	// Convert the decoded bytes to a text string
	decodedText := string(decodedBytes)
	// Print the decoded text