Go Base64
In Go (also known as Golang), the standard library supports base64 encoding and decoding through the package "encoding/base64". The package provides two functions, EncodeToString()
and DecodeString()
, which can be used to encode and decode data to and from a base64 string, respectively.
The base64 encoding and decoding functions were added to the Go standard library in version 1.4. So, these methods are available in all versions of Go starting from Go 1.4.
The base64 alphabet consists of 64 characters, each represented by a 6-bit value. The characters used in the base64 alphabet and their corresponding values in the sequential form are as follows:
Character | Value | Character | Value |
---|---|---|---|
A | 0 | g | 32 |
B | 1 | h | 33 |
C | 2 | i | 34 |
D | 3 | j | 35 |
E | 4 | k | 36 |
F | 5 | l | 37 |
G | 6 | m | 38 |
H | 7 | n | 39 |
I | 8 | o | 40 |
J | 9 | p | 41 |
K | 10 | q | 42 |
L | 11 | r | 43 |
M | 12 | s | 44 |
N | 13 | t | 45 |
O | 14 | u | 46 |
P | 15 | v | 47 |
Q | 16 | w | 48 |
R | 17 | x | 49 |
S | 18 | y | 50 |
T | 19 | z | 51 |
U | 20 | 0 | 52 |
V | 21 | 1 | 53 |
W | 22 | 2 | 54 |
X | 23 | 3 | 55 |
Y | 24 | 4 | 56 |
Z | 25 | 5 | 57 |
a | 26 | 6 | 58 |
b | 27 | 7 | 59 |
c | 28 | 8 | 60 |
d | 29 | 9 | 61 |
e | 30 | + | 62 |
f | 31 | / | 63 |
Note: The last two characters, "+" and "/", are used for padding when the input data does not have a length that is a multiple of 3 bytes. The "=" character is used as a padding character to indicate that the data has been padded.
The Go standard library provides two sets of functions for base64 encoding and decoding. The first set is Encode()
and Decode()
methods, and the second set is EncodeToString()
and DecodeString()
.
Encoding a string in Golang using EncodeToString()
The following example shows how to encode a string to a base64 encoded string. Here is a breakdown of the code:
- The package
encoding/base64
is imported to provide access to the base64 encoding and decoding functions. - The original string
Hello, World!
is stored in the variabledata
. - The
EncodeToString()
function from theStdEncoding
package encodes the byte slice ofdata
to a base64 encoded string. - The encoded string is then printed to the console using the
fmt
package.
package main
import (
"encoding/base64"
"fmt"
)
func main() {
data := "Hello, World!"
encoded := base64.StdEncoding.EncodeToString([]byte(data))
fmt.Println("The base64 encoded version is:", encoded)
}
Output
The base64 encoded version is: SGVsbG8sIFdvcmxkIQ==
Decoding a string in Golang using DecodeToString()
The following example shows how to decode a base64 encoded string back to its original form. Here is a breakdown of the code:
- The package
encoding/base64
is imported to provide access to the base64 encoding and decoding functions. - The base64 encoded string
SGVsbG8sIFdvcmxkIQ==
is stored in the variabledata
. - The
DecodeString()
function from theStdEncoding
package is used to decode thedata
to its original form. - The decoded byte slice is then converted to a string and printed to the console using the
fmt
package.
package main
import (
"encoding/base64"
"fmt"
)
func main() {
data := "SGVsbG8sIFdvcmxkIQ=="
decoded, _ := base64.StdEncoding.DecodeString(data)
fmt.Println("The decoded version of the string is:", string(decoded))
}
Output
The decoded version of the string is: Hello, World!
Encode()
and Decode()
functions expect the input data to be in the form of a byte slice, and the encoded/decoded output is also returned as a byte slice.
Decoding a string in Golang using Encode()
In the following example, the Encode()
function is called with the encoded byte slice, which returns the encoded byte slice, which is then converted to a string and printed to the console using the fmt
package.
package main
import (
"encoding/base64"
"fmt"
)
func main() {
text := "Hello, World!"
data := make([]byte, base64.StdEncoding.EncodedLen(len(text)))
base64.StdEncoding.Encode(data, []byte(text))
fmt.Println(data)
encoded := string(data)
fmt.Println(encoded)
}
Output
[83 71 86 115 98 71 56 115 73 70 100 118 99 109 120 107 73 81 61 61]
SGVsbG8sIFdvcmxkIQ==
Decoding a string in Golang using Decode()
In the following example, the Decode()
function is called with the encoded byte slice, which returns the decoded byte slice, which is then converted to a string and printed to the console using the fmt
package.
package main
import (
"encoding/base64"
"fmt"
)
func main() {
text := "SGVsbG8sIFdvcmxkIQ=="
data := make([]byte, base64.StdEncoding.DecodedLen(len(text)))
base64.StdEncoding.Decode(data, []byte(text))
fmt.Println(data)
decoded := string(data)
fmt.Println(decoded)
}
Output
[72 101 108 108 111 44 32 87 111 114 108 100 33 0 0]
Hello, World!
Conclusion
In conclusion, the Go standard library provides two sets of functions for base64 encoding and decoding. The first set is Encode
and Decode
and the second set is EncodeToString
and DecodeString
.
The Encode
function returns an Encoding type that can be used to encode data, and the Decode
function expects a byte slice as an argument, which contains the encoded data to be decoded, and returns the decoded byte slice and an error.
The EncodeToString
and DecodeString
functions are more convenient when working with string data, as they eliminate the need to convert between strings and byte slices.