Go by Example:: Base64 Encoding
Go by Example:: Base64 Encoding
Go provides built-in
support for base64
encoding/decoding.
package main
func main() {
uEnc := b64.URLEncoding.EncodeToString([]byte(data))
This encodes/decodes using fmt.Println(uEnc)
a URL-compatible base64 uDec, _ := b64.URLEncoding.DecodeString(uEnc)
format. fmt.Println(string(uDec))
}
The string encodes to slightly different values with the standard and URL base64 encoders (trailing
+ vs -) but they both decode to the original string as desired.
package main
import "fmt"
import "net"
import "net/url"
func main() {
u, err := url.Parse(s)
Parse the URL and ensure if err != nil {
there are no errors. panic(err)
}
Running our URL parsing program shows all the different pieces that we extracted.
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
)
func main() {
There is no built-in
_, err = f.Seek(0, 0)
rewind, but Seek(0, check(err)
0) accomplishes this.