January 2018
Intermediate to advanced
340 pages
8h 6m
English
In the previous example, the QR code was base64 encoded. Since this is a common task, it is worth covering how to encode as well as decode. Any time binary data needs to be stored or transmitted as a string, base64 encoding is useful.
This example demonstrates a very simple use case of encoding and decoding a byte slice. The two important functions for base64 encoding and decoding are EncodeToString() and DecodeString():
package mainimport ( "encoding/base64" "fmt" "log")func main() { data := []byte("Test data") // Encode bytes to base64 encoded string. encodedString := base64.StdEncoding.EncodeToString(data) fmt.Printf("%s\n", encodedString) // Decode base64 encoded string to bytes. decodedData, err := base64.StdEncoding.DecodeString(encodedString) ...Read now
Unlock full access