How to Use GoLang Base64 Package Encoding and Decoding

Step-by-step guide on how to use GoLang encoding/base64 package for base64 encoding and decoding. Perfect for web developers and GoLang programmers.
On this page

How to Use GoLang Base64 Package Encoding and Decoding

Excerpt

Step-by-step guide on how to use GoLang encoding/base64 package for base64 encoding and decoding. Perfect for web developers and GoLang programmers.

Introduction

The GoLang base64 package is an in-built package for encoding and decoding Base64 in GoLang programming. This article will provide a step-by-step guide on how to use the GoLang’s encoding/base64to encode and decode strings.

Tutorial

Importing the GoLang encoding/base64 package

The GoLang programming language features a built-in package for Base64 encoding and decoding, known as “encoding/base64”. This package provides functionality for encoding and decoding data in Base64 format. To use this package, you can import it into your GoLang program.

To import the “encoding/base64” package, you can use the import statement followed by the package name:

1import "encoding/base64"

The “encoding/base64” package is part of the GoLang standard library, which is always available and doesn’t require any additional installation or setup.

Base64 Encoding with GoLang encoding/base64 package

Once we have imported the package, we can use the base64.StdEncoding.EncodeToString() function to encode data into base64 format. This function takes a byte slice as input and returns a string containing the base64-encoded data.

Let’s take an example of encoding a string using the EncodeToString() function:

 1package main
 2
 3import (
 4    "fmt"
 5    "encoding/base64"
 6)
 7
 8func main() {
 9    message: = "Hello, World!"
10    encodedMessage: = base64.StdEncoding.EncodeToString([] byte(message))
11    fmt.Println(encodedMessage)
12}

Output Result

1SGVsbG8sIFdvcmxkIQ==

Base64 Decoding with GoLang encoding/base64 package

Base64 decoding is the process of converting a base64-encoded string back to its original form. In GoLang programming, this can be easily achieved using the encoding/base64 package. The base64.StdEncoding.DecodeString() function decodes a base64-encoded string and returns the decoded data as a byte slice.

Here’s an example:

 1package main
 2
 3import (
 4    "encoding/base64"
 5    "fmt"
 6)
 7
 8func main() {
 9    encodedData: = "SGVsbG8gV29ybGQh" // Base64-encoded string
10    decodedData,
11    err: = base64.StdEncoding.DecodeString(encodedData)
12    if err != nil {
13        fmt.Println("error:", err)
14        return
15    }
16    fmt.Println(string(decodedData))
17}

In this example, we have a base64-encoded string “SGVsbG8gV29ybGQh”, which represents the text “Hello World!”.

We then convert this byte slice to a string using the string() function and print it on the console. The output will be:

1Hello World!

Conclusion

The GoLang “encoding/base64” package allows you to encode and decode data using built-in functions, with just a few lines of code.

Its ease of use, reliability, and built-in nature within the GoLang language mean that there’s no need to rely on third-party libraries or worry about compatibility issues with different versions of GoLang.

An online base64 tool to quickly verify your answers