
Golang Base64 encoding and decoding is a fundamental skill every developer should have in their toolbox. It's used to safely transmit data over the internet, and it's surprisingly simple to do in Golang.
Base64 encoding converts binary data into a text format that can be easily transmitted over the internet. This is particularly useful for encoding binary data, such as images or videos, into a format that can be safely sent over HTTP.
In Golang, the `encoding/base64` package provides functions for base64 encoding and decoding. The `StdEncoding` variable from this package contains the standard base64 encoding scheme used by the internet. This scheme replaces binary data with a text representation, making it safe to transmit.
Base64 encoding is commonly used for encoding binary data in URLs, as well as for encoding data in HTTP headers. It's also used in JSON Web Tokens (JWTs) to encode user data.
Recommended read: Base64 Img Src
Constants
In Go, there are two main constants for base64 encoding: RawURLEncoding and StdEncoding. RawURLEncoding is the unpadded alternate base64 encoding defined in RFC 4648.
Recommended read: Openssl Base64
This encoding is typically used in URLs and file names, making it a great choice for web development. StdEncoding, on the other hand, is the standard base64 encoding, as defined in RFC 4648.
Both RawURLEncoding and StdEncoding are widely used in the industry, and choosing the right one depends on your specific needs. RawURLEncoding omits padding characters, which can be beneficial for certain use cases.
Encoding
Encoding is a crucial process in Go that converts binary data to text using Base64 encoding. This process is useful when you need to send binary data over a medium that requires only textual information.
The base64 package in Go provides the EncodeToString function for encoding binary data to Base64. This function takes a byte slice as input and returns a string.
Base64 encoding operates in 4-byte blocks, and when finished writing, the caller must Close the returned encoder to flush any partially written blocks. The StdEncoding variable is a *Encoding type that implements the Encoding interface, which provides the EncodeToString function.
You might enjoy: Go vs Golang
The EncodeToString function is useful when you need to encode a string to Base64 encoding. This function returns the base64 encoding of the input string.
Base64 encoding is useful when you need to store binary data in a text format. The RawStdEncoding variable is a *Encoding type that implements the Encoding interface, which provides the EncodeToString and DecodeString functions.
The EncodeToString function can be used to encode a string to Base64 encoding, which represents the binary data of the original string in text form using some specific ASCII characters.
For more insights, see: Data Text Html Base64
Decoding
Decoding is a crucial step in working with base64 in Go. You can use the NewDecoder function to construct a new base64 stream decoder.
To decode a base64-encoded string, you can use the DecodeString function, which returns the bytes represented by the base64 string. New line characters are ignored during the decoding process.
The Decode function decodes src using the encoding enc and writes at most Encoding.DecodedLen(len(src)) bytes to dst. The caller must ensure that dst is large enough to hold all the decoded data.
If this caught your attention, see: Golang Reflect to Call Function in Package
You can use the DecodedLen function to get the maximum length in bytes of the decoded data corresponding to n bytes of base64-encoded data. This can be useful for allocating enough space for the decoded data.
The base64.StdEncoding.Decode function decodes the source slice of bytes to the destination slice of bytes. This function is similar to the Decode function but uses the standard encoding.
See what others are reading: Golang Copy Slice
Functions
The Go encoding/base64 package provides several functions for encoding and decoding binary data using base64.
The Encode function takes a byte slice as input and returns a base64 encoded string. This is useful for encoding binary data before sending it over a network.
The EncodeToString function is similar to Encode but returns the encoded string directly. It's a convenient function when you don't need to store the original byte slice.
The Decode function takes a base64 encoded string as input and returns the original byte slice. This is useful for decoding data received from a network or a file.
See what others are reading: Golang Encode Webp
The DecodeString function is similar to Decode but returns the original byte slice directly. It's a convenient function when you don't need to store the original string.
The NewDecoder function returns a decoder that can decode multiple base64 encoded strings from a reader. This is useful when you need to decode a large amount of data from a file or a network connection.
The NewEncoder function returns an encoder that can encode multiple byte slices to base64 strings. This is useful when you need to encode a large amount of data before sending it over a network.
Intriguing read: Define a Map of Custom Schema Data Type Golang
Error Handling
Error handling is crucial when working with base64 in Golang.
Incorrect base64 strings can cause decoding faults, which must be handled as potential errors.
The DecodeString() function can be used to decode faulty base64-encoded strings.
A simple if statement can handle the function's error, which results from the string's inability to be a valid base64 string.
The error message will be "illegal base64 data at input byte 4" and will be reported to the console.
Discover more: Golang Create Error
String Handling
String Handling is a crucial aspect of working with base64 in Go, and it's surprisingly simple. You can decode a base64-encoded string using the DecodeString() function.
The base64-encoded string is converted back to its original representation, which is a string containing the binary data. This is exactly what happens in the example where "VGhpcyBpcyBhIHRlc3Q=" is decoded to "This is a test".
The process of base64 encoding and decoding is a two-way street. The original string data is converted to a Base64 encoded string, and then decoded back to its initial representation. This is a straightforward process that can be accomplished using the base64.StdEncoding.EncodeToString and base64.StdEncoding.DecodeString functions.
A different take: Declate a Map of String and Value as Map Golang
Basic String
Basic String Handling involves converting original string data into a Base64 encoded string, which represents the binary data of the original string in text form using specific ASCII characters.
The process of encoding converts the original string into a Base64 encoded string, which can be easily transmitted or stored.
To encode a string, you can use the base64.StdEncoding.EncodeToString function, which returns the base64 encoding of a string.
Conversely, when you need to convert a Base64 encoded string back to its original representation, you can use the base64.StdEncoding.DecodeString function, which returns the bytes represented by the Base64 encoded string.
Decoding a Base64 encoded string involves using a function like DecodeString(), which takes the encoded string as input and returns a byte slice containing the decoded binary data.
The decoded binary data can then be converted to a string using the string() function and printed to the terminal, revealing the original string.
For more insights, see: Read a Custom Resource Using Cynamic Client Golang
UTF-8 for Multilingual Data
UTF-8 is a widely used encoding standard for handling multilingual data.
Go natively supports UTF-8, making it easy to encode UTF-8 strings to Base64.
Encoding a UTF-8 string to Base64 in Golang doesn't differ from encoding any other string.
One only needs to change the base64 value into bytes and eventually cast it again into a string.
Go will do the rest, using its own UTF-8 coding to ensure accurate conversion.
If this caught your attention, see: Golang Go
File Handling
File handling in Go is a crucial aspect of working with base64 encoding.
The `encoding/base64` package provides a `NewDecoder` function that allows you to decode base64 strings, which can be useful when handling files.
In Go, you can use the `ioutil` package to read and write files, making it easy to integrate base64 encoding into your file handling code.
The `ioutil.ReadFile` function can be used to read the contents of a file, which can then be encoded using the `encoding/base64` package.
Base64 encoding can be useful for handling large files, as it can reduce the size of the file by up to 33%.
Readers also liked: Install Golang Package
Padding
In Go, you can customize how base64 encoding handles padding with the WithPadding method. This method creates a new encoding with a specified padding character, or disables padding altogether.
The padding character must be a rune that's equal to or below '\xff', and it can't be '\r' or '
', or contained in the encoding's alphabet. It also can't be negative.
When using WithPadding, keep in mind that padding characters above '\x7f' are encoded as their exact byte value, rather than using the UTF-8 representation of the codepoint. This is a key detail to consider when choosing a padding character.
You might enjoy: How to Update a Github Using Golang
With Padding Added

The WithPadding function creates a new encoding identical to the original one, but with a specified padding character or NoPadding to disable padding altogether.
You can choose any padding character as long as it's not '\r' or '
', and it's not already part of the encoding's alphabet. This ensures that the padding character won't be confused with other characters in the encoding.
The padding character must also be a rune that's equal to or below '\xff'. This means it can't be a character that would require more than one byte to encode in UTF-8.
If you choose a padding character above '\x7f', it will be encoded as its exact byte value rather than using the UTF-8 representation of the codepoint. This is an important consideration when working with encodings and padding.
Additional reading: T Golang
Without Padding
In some cases, padding might not be desirable, especially when space is limited or when encoding URL links. This is where Base64 encoding without padding comes in.

You can use the WithPadding function in Golang to encode and decode Base64 data without padding characters. This is achieved by using StdEncoding.WithPadding(base64.NoPadding) to build an encoder that doesn't append any kind of padding character.
The decoding process is straightforward and doesn't require specifying "no padding" while doing so. Information indicating the absence of pad bits can always be derived from the encoded message directly.
Not all decoders may accept unpadded Base64 data, especially those used in other languages or servers, so it's essential to ensure consistency in your Base64 version.
Other Types
In the Base64 package, you'll find a few types that are worth mentioning. The Encoding type represents a base64 encoding/decoding scheme, defined by a 64-character alphabet.
The most common usage of Encoding is via the standard predefined Encodings StdEncoding and URLEncoding. This makes it easy to work with base64 data in your Go programs.
The CorruptInputError type is used to report that the input is not valid base64 data. This error can be caught and handled in your code to provide a better user experience.
Here are the types you'll find in the Base64 package:
- Encoding - represents a base64 encoding/decoding scheme
- CorruptInputError - reports invalid base64 data
Featured Images: pexels.com


