Encode and Decode Strings using Base 64

· 99 words · 1 minute read

The example below shows to how to encode and then subsequently decode a string using base 64. Doing this has many uses, one of which to safely encode byte data in structures like JSON.

We use the encoding/base64 package to do this, which takes in and returns a byte array into it's EncodeToString and DecodeString methods.

package main

import (

func main() {

    myString := "This is golangcode.com testing base 64!"

    // Encode
    encodedString := base64.StdEncoding.EncodeToString([]byte(myString))
    fmt.Printf("Encoded: %s\n", encodedString)

    // Decode
    raw, err := base64.StdEncoding.DecodeString(encodedString)
    if err != nil {

    fmt.Printf("Decoded: %s\n", raw)

Image of Author Edd Turtle

Author:  Edd Turtle

Edd is the Lead Developer at Hoowla, a prop-tech startup, where he spends much of his time working on production-ready Go and PHP code. He loves coding, but also enjoys cycling and camping in his spare time.

See something which isn't right? You can contribute to this page on GitHub or just let us know in the comments below - Thanks for reading!