Base64 Package in Golang


Base64 package in Golang is a standard library package that offers capabilities for base64-encoding and -decoding binary data. Base64 is a popular method of encoding that enables binary data to be represented using just sharable ASCII letters, making it perfect for transmission over text-based protocols like HTTP and SMTP.

We'll look into the Golang base64 package and discover how to encrypt and decrypt data in base64 format using the package's functions. We'll also talk about the best ways to interact with binary data in Golang and examine some typical use cases for base64 encoding and decoding. You ought to be able to utilise the base64 package in your own Golang projects after reading this article.

Understanding Base64 Encoding

Let's quickly go over base64 encoding and its benefits before delving into the Go base64 package.

The Base64 family of binary-to-text encoding techniques uses ASCII strings to represent binary data. Base64 encoding is used to transform binary data into a form that can be sent over a network simply, like in email attachments or HTTP requests. It is frequently employed for both data storage and encryption.

Base64 encoding converts three 8-bit bytes to four 6-bit values, which are subsequently expressed as ASCII characters. There is no data loss while transmission over various platforms, despite the fact that the final string is longer than the original binary data.

Using the Base64 Package in Golang

Data in base64 format can be encoded and decoded using the base64 package in Go. Encode and Decode are the two primary features offered by the package. Let's look at how to employ these features.

Encoding Data in Base64 Format

The base64-encoded string that the Encode function returns is created from an input byte slice. Here's an illustration −

Example

package main

import (
   "encoding/base64"
   "fmt"
)

func main() {
   data := []byte("Hello, world!")
   str := base64.StdEncoding.EncodeToString(data)
   fmt.Println(str)
}

Output

SGVsbG8sIHdvcmxkIQ==

In this example, we begin by defining a byte slice that contains the text "Hello, world!" The data is then encoded in base64 format using the EncodeToString method from the base64 package. The string that results is then output to the console.

Data from Base64 Format Decoding

A base64-encoded string is passed to the Decode function, which returns a byte slice. Here's an illustration −

Example

package main

import (
   "encoding/base64"
   "fmt"
)

func main() {
   str := "SGVsbG8sIHdvcmxkIQ=="
   data, err := base64.StdEncoding.DecodeString(str)
   if err != nil {
      fmt.Println("Error:", err)
      return
   }
   fmt.Println(string(data))
}

Output

Hello, world!

In this example, we define a base64 encoded string and use the DecodeString function from the base64 package to decode the data back into a byte slice. The resulting byte slice is then converted to a string and printed to the console.

Examples in Real Life for the Base64 Package

There are several advantageous uses for the base64 package. It can be utilised, for instance, to store binary data in a text-based format or to encrypt data for HTTP requests. These are a few instances drawn from real-world situations −

There are several advantageous uses for the base64 package. It can be utilised, for instance, to store binary data in a text-based format or to encrypt data for HTTP requests. These are a few instances drawn from real-world situations −

Encoding Images for Use in HTML

package main

import (
   "encoding/base64"
   "fmt"
   "io/ioutil"
)

func main() {
   // Read image file into byte slice
   data, err := ioutil.ReadFile("image.png")
   if err != nil {
      fmt.Println("Error:", err)
      return
   }
}

Conclusion

Data in base64 format can be encoded and decoded quickly and easily using the base64 package in Golang. Developers may simply convert data to and from base64 using its methods, which also take care of padding and line breaks. The package is commonly used in web applications and other contexts where text-based protocols must be utilised to convey binary data. You should be able to utilise the base64 package in your own Golang projects successfully after reading this tutorial.

Updated on: 06-Apr-2023

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements