DevToolsForYou

Base64 Encoder and Decoder in Go — Code Examples

Base64 Encoder and Decoder in GoUse the online tool →

Base64 encoding converts binary or text data into a 64-character ASCII alphabet so it can be safely transmitted over text-only channels. Here is how to encode and decode Base64 in each language using only the standard library.

Go's encoding/base64 package provides StdEncoding for standard Base64 and URLEncoding for the URL-safe variant.

Go
package main

import (
    "encoding/base64"
    "fmt"
)

func main() {
    // Encode
    input := "Hello, world!"
    encoded := base64.StdEncoding.EncodeToString([]byte(input))
    fmt.Println(encoded) // SGVsbG8sIHdvcmxkIQ==

    // Decode
    decoded, err := base64.StdEncoding.DecodeString("SGVsbG8sIHdvcmxkIQ==")
    if err != nil {
        panic(err)
    }
    fmt.Println(string(decoded)) // Hello, world!

    // URL-safe Base64 (no + or /)
    urlEncoded := base64.URLEncoding.EncodeToString([]byte(input))
    fmt.Println(urlEncoded)

    // URL-safe without padding
    rawEncoded := base64.RawURLEncoding.EncodeToString([]byte(input))
    fmt.Println(rawEncoded)
}
Notes & gotchas
  • Use base64.URLEncoding for JWT and URL contexts.
  • base64.RawURLEncoding omits padding — required by the JWT spec.
  • DecodeString returns an error for malformed input; always handle it.
Try it in your browser

Need to base64 encode/decode without writing code? The Base64 Encoder and Decoder runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open Base64 Encode/Decode
Base64 Encoder and Decoder in other languages