W3Basic Logo

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:

CharacterValueCharacterValue
A0g32
B1h33
C2i34
D3j35
E4k36
F5l37
G6m38
H7n39
I8o40
J9p41
K10q42
L11r43
M12s44
N13t45
O14u46
P15v47
Q16w48
R17x49
S18y50
T19z51
U20052
V21153
W22254
X23355
Y24456
Z25557
a26658
b27759
c28860
d29961
e30+62
f31/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 variable data.
  • The EncodeToString() function from the StdEncoding package encodes the byte slice of data 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 variable data.
  • The DecodeString() function from the StdEncoding package is used to decode the data 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.

© 2023 W3Basic. All rights reserved.

Follow Us: