Golang base64 encode example

Golang provides built in support for base64 encoding/decoding. Directly in the standard library we have "encoding/base64" so there is no need to download or install third-party libraries. Let’s have a look at the Go base64 encoding documentation https://golang.org/pkg/encoding/base64/. In order to encode or decode a string to and from base64 we need to use the type Encoder which provides the following methods

func (enc *Encoding) DecodeString(s string) ([]byte, error)

and to encode a string (in this case a slice of bytes) to base64 format

func (enc *Encoding) EncodeToString(src []byte) string

the standard library also provides us methods that decode and encode from and to byte slices for more general purpose usages (Decode, Encode).

Here is an example on how to encode and decode base64 strings

Here is the expected output

SGVsbG8gV29ybGQu
Hello World.

Want a job board with no recruiters and clear salary ranges?
Find your next Go Job on Golang Cafe