Golang encoding/base64.Encoding.Decode function examples

package encoding/base64

Decode decodes src(2nd parameter) using the encoding enc. It writes at most DecodedLen(len(src)) bytes to dst(1st parameter) and returns the number of bytes written. If src contains invalid base64 data, it will return the number of bytes successfully written and CorruptInputError. New line characters (\r and \n) are ignored.

Golang encoding/base64.Encoding.Decode function usage examples

Example 1 :

 var value []byte
 decoded := make([]byte, base64.URLEncoding.DecodedLen(len(value)))
 b, err := base64.URLEncoding.Decode(decoded, value)

Example 2 :

 authStr = "Mr. Tony"
 decLen := base64.StdEncoding.DecodedLen(len(authStr))
 decoded := make([]byte, decLen)
 authByte := []byte(authStr)
 n, err := base64.StdEncoding.Decode(decoded, authByte)

References :

http://golang.org/pkg/encoding/base64/#Encoding.Decode

https://www.socketloop.com/references/golang-encoding-base32-encoding-decode-function-example

Advertisement