Base64.DecodeString() Function in Golang With Examples


The base64 package in Golang provides several functions for encoding and decoding binary data in base64 format. DecodeString(), one of the most often used functions, decodes a base64-encoded string and returns the original binary data.

We will thoroughly examine the DecodeString() method and discover how to decode base64-encoded data in Golang. We'll start by talking about the function's fundamental syntax and parameters before looking at some real-world applications. You ought to be able to utilise the DecodeString() function in your own Golang projects after reading this article.

Basic Syntax of base64.DecodeString()

The basic syntax of the DecodeString() function is as follows −

func DecodeString(s string) ([]byte, error)

The base64-encoded string that has to be decoded is the only input for the function. If the decoding operation fails, it provides a byte slice with the decoded binary data together with a non-nil error value.

Example 1: Decoding a Simple Base64 String

Let's begin with a straightforward illustration showing how to decode a base64-encoded string using the DecodeString() function −

package main

import (
   "encoding/base64"
   "fmt"
)

func main() {
   encodedString := "VGhpcyBpcyBhIHRlc3Q="
   decodedBytes, err := base64.StdEncoding.DecodeString(encodedString)
   if err != nil {
      fmt.Println("Error decoding string:", err)
      return
   }
   fmt.Println(string(decodedBytes))
}

Output

This is a test

The base64-encoded string "VGhpcyBpcyBhIHRlc3Q=" is defined in this example, and it is decoded using the DecodeString() method. A byte slice containing the decoded binary data is returned by the function, which we then convert to a string using the string() function and print to the terminal. The output must be the original "This is a test" base64-encoded string, which should show up on the screen.

Example 2: Handling Decoding Errors

Incorrect base64 strings or other problems that prevent the data from being correctly decoded must be handled as potential decoding faults. Here is an illustration of how to use the DecodeString() function to solve decoding issues −

package main

import (
   "encoding/base64"
   "fmt"
)

func main() {
   invalidString := "this is not a valid base64 string"
   decodedBytes, err := base64.StdEncoding.DecodeString(invalidString)
   if err != nil {
      fmt.Println("Error decoding string:", err)
      return
   }
   fmt.Println(string(decodedBytes))
}

Output

Error decoding string: illegal base64 data at input byte 4

The DecodeString() function is used to decode the faulty base64-encoded string that was defined in this example. A simple if statement can handle the function's error that results from the string's inability to be a valid base64 string. The string is incorrect base64, as shown by the error message, which should be "illegal base64 data at input byte 4" and is reported to the console.

Conclusion

Decoding base64-encoded data in Golang is made possible by the DecodeString() method. You should be able to utilise the function in your own Golang projects and deal with any potential decoding issues after studying the examples given in this article. To make your code stable and trustworthy, always remember to handle errors correctly.

Updated on: 06-Apr-2023

651 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements