Go

base64 Encode / Decode

admin by @admin ADMIN
8m ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
`encoding/base64` has Standard, URL-safe, and Raw (no padding) variants. URL-safe replaces `+/` with `-_` so the output is safe in query strings and filenames.
Go
Raw
package main

import (
    "encoding/base64"
    "fmt"
)

func main() {
    raw := []byte("hello, world")

    // Standard encoding (with padding =)
    enc := base64.StdEncoding.EncodeToString(raw)
    fmt.Println(enc)                                 // aGVsbG8sIHdvcmxk

    dec, err := base64.StdEncoding.DecodeString(enc)
    if err == nil { fmt.Println(string(dec)) }       // hello, world

    // URL-safe + no padding (good for filenames, query strings, JWTs)
    safe := base64.RawURLEncoding.EncodeToString(raw)
    fmt.Println(safe)                                // aGVsbG8sIHdvcmxk (here it happens to match)

    // Generate a random URL-safe token
    token := base64.RawURLEncoding.EncodeToString([]byte("32 random bytes here..."))
    fmt.Println(token)
}
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.