Introduction
Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format. It is commonly used to encode binary data, such as images or files, into a text format that can be easily transmitted over text-based protocols like HTTP. Go provides built-in support for Base64 encoding and decoding through the encoding/base64
package. In this chapter, you will learn how to encode and decode data using Base64 in Go.
Base64 Encoding
Example: Encoding a String
To encode a string to Base64, you can use the base64.StdEncoding.EncodeToString
function.
Example:
package main
import (
"encoding/base64"
"fmt"
)
func main() {
data := "Hello, World!"
encoded := base64.StdEncoding.EncodeToString([]byte(data))
fmt.Println("Encoded:", encoded)
}
In this example, the string "Hello, World!" is encoded to a Base64 string.
Example: Encoding Binary Data
You can also encode binary data, such as the contents of a file.
Example:
package main
import (
"encoding/base64"
"fmt"
"io/ioutil"
"log"
)
func main() {
data, err := ioutil.ReadFile("example.png")
if err != nil {
log.Fatal(err)
}
encoded := base64.StdEncoding.EncodeToString(data)
fmt.Println("Encoded:", encoded)
}
In this example, the contents of example.png
are read and encoded to a Base64 string.
Base64 Decoding
Example: Decoding a Base64 String
To decode a Base64 string back to its original form, use the base64.StdEncoding.DecodeString
function.
Example:
package main
import (
"encoding/base64"
"fmt"
"log"
)
func main() {
encoded := "SGVsbG8sIFdvcmxkIQ=="
decoded, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
log.Fatal(err)
}
fmt.Println("Decoded:", string(decoded))
}
In this example, the Base64 string "SGVsbG8sIFdvcmxkIQ==" is decoded back to its original string form.
Example: Decoding Binary Data
You can decode Base64-encoded binary data back to its original form.
Example:
package main
import (
"encoding/base64"
"io/ioutil"
"log"
)
func main() {
encoded := "iVBORw0KGgoAAAANSUhEUgAAAAUA..."
data, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile("decoded.png", data, 0644)
if err != nil {
log.Fatal(err)
}
log.Println("File decoded.png written successfully")
}
In this example, the Base64-encoded string is decoded and written back to a file named decoded.png
.
URL-Safe Base64 Encoding
Go also provides URL-safe Base64 encoding, which replaces +
with -
and /
with _
to make the encoded string safe for use in URLs.
Example: URL-Safe Base64 Encoding
Example:
package main
import (
"encoding/base64"
"fmt"
)
func main() {
data := "Hello, World!"
encoded := base64.URLEncoding.EncodeToString([]byte(data))
fmt.Println("URL-Safe Encoded:", encoded)
}
In this example, the string "Hello, World!" is encoded to a URL-safe Base64 string.
Example: URL-Safe Base64 Decoding
Example:
package main
import (
"encoding/base64"
"fmt"
"log"
)
func main() {
encoded := "SGVsbG8sIFdvcmxkIQ=="
decoded, err := base64.URLEncoding.DecodeString(encoded)
if err != nil {
log.Fatal(err)
}
fmt.Println("Decoded:", string(decoded))
}
In this example, the URL-safe Base64 string "SGVsbG8sIFdvcmxkIQ==" is decoded back to its original string form.
Base64 Encoding and Decoding with Buffer
For larger data, you may want to use a buffer for more efficient encoding and decoding.
Example: Encoding with Buffer
Example:
package main
import (
"encoding/base64"
"bytes"
"fmt"
)
func main() {
data := []byte("Hello, World!")
var buf bytes.Buffer
encoder := base64.NewEncoder(base64.StdEncoding, &buf)
encoder.Write(data)
encoder.Close()
fmt.Println("Encoded:", buf.String())
}
In this example, data is written to a buffer using a Base64 encoder.
Example: Decoding with Buffer
Example:
package main
import (
"encoding/base64"
"bytes"
"fmt"
"io"
"log"
)
func main() {
encoded := "SGVsbG8sIFdvcmxkIQ=="
buf := bytes.NewBufferString(encoded)
decoder := base64.NewDecoder(base64.StdEncoding, buf)
decoded, err := io.ReadAll(decoder)
if err != nil {
log.Fatal(err)
}
fmt.Println("Decoded:", string(decoded))
}
In this example, data is read from a buffer using a Base64 decoder.
Conclusion
Base64 encoding and decoding in Go is straightforward using the encoding/base64
package. You can encode and decode strings, binary data, and even use URL-safe Base64 encoding when needed. By understanding these basic operations, you can effectively handle Base64 encoding and decoding in your Go applications.